0

I am from java/C# background and I am trying to understand some C++ code. I learned that a code like following is defining a function out of the class file

Some_Class::someFunction ( void ){
//SOME LINE
//OF CODE
//FOR THIS FUNCTION
}

I am using Visual Studio 2010 to build the project. However, when I debug it to understand the flow, I see something like abc::def::ghk::The_Class::theFunction() at line 999 in Call Stack of VS. Now, I am able to find The_Class::theFunction( void ){//SOME CODE} definition in a class file Strange.cxx.

Based on my learning I think abc, def and ghk are classes but I am unable to find a definition like class abc or class def.

My question is why Call Stack in VS show like this and what's their purpose/meaning?

Karan
  • 752
  • 2
  • 13
  • 34
  • 1
    Is `The_Class` embedded in any namespaces or other classes? Nothing to do with the call stack, just how symbols are defined in c++. – πάντα ῥεῖ Sep 02 '20 at 20:24
  • This should be `Some_Class::SomeFunction` without the interleaving spaces, they're all part of one thing. It's also odd you have both underscore-style names like `Some_Class` and camel-case like `SomeFunction`. Pick one style and stick to it. The second form is far more conventional. – tadman Sep 02 '20 at 20:28
  • Please put more information of the debugging output. It's hard to answer like this. – y_159 Sep 02 '20 at 20:29
  • 2
    jumping into c++ like this isnt the best strategy to learn it. Coincidentally the simple "Hello World" already uses a namespace, just saying. – 463035818_is_not_an_ai Sep 02 '20 at 20:30
  • 2
    In `abc::def::ghk::The_Class::The_Function()` `abc` could be a class or a namespace. No way to know without code. – user4581301 Sep 02 '20 at 20:34
  • 2
    @tadman — to be clear, the spaces around `::` are okay. You’re giving **style advice**, which should not be phrased as “[t]his should be. – Pete Becker Sep 02 '20 at 20:34
  • @PeteBecker There's a lot of things that C++ is cool with that are really bad ideas. Just trying to steer in the direction of convention here. It is style advice, though, so I'll try and phrase it as such. – tadman Sep 02 '20 at 20:36
  • 1
    Reiterating @idclev463035818 's comment: Logic is logic, but the way you describe and the best practices for framing that logic vary greatly from language to language. You can apply the programming fundamentals learned in Java and C#, but trying to write C++ code the way you write C# or Java will lead to much frustration. You aren't coming in cold with no experience, so [get some suitable texts and references to help your learning](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Sep 02 '20 at 20:41
  • Please refer to your C++ book! It will teach you about namespaces. – Asteroids With Wings Sep 02 '20 at 21:06
  • Thank you all for your valuable comments. What I have seen is that styling is for easy understanding. If its consistent in your organization and they can easily understand that then whatever you choose is okay. This question was not about styling however I edited the question. – Karan Sep 02 '20 at 21:33

2 Answers2

2

:: is the scope resolution operator. I suppose this wont help you much, so I will try to put it in simple terms.

abc::def::ghk::The_Class::The_Function()

This is the full name (*) of The_Function(). Suppose it is a method of The_Class as in your snippet. Then this class might be nested inside another class called ghk:

struct ghk {
    struct The_Class {
        void The_Function();
    };
};

You can nest classes inside nested classes until the doctor comes.

Next, C++ uses namespaces to organize names. For example all standard library is in the namespace std, the boost library puts its names in the namespace boost. In this way you can have a foo in boost and a foo in the standard library and you can still tell them apart because their full name is boost::foo and std::foo. Namespaces can be nested as well.

One hypothetical declaration of The_Function could be

namespace abc {
namespace def {
struct ghk {
    struct The_Class {
        void The_Function();
    };
};
}
}
  • = The technical term is "fully qualified name", but actually the fully qualified name has another :: in front. For example ::foo is the name of a foo declared in the global scope (it is not placed inside a namespace).
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

My question is why Call Stack in VS show like this ...

Because the call stack shows you the fully qualified function symbol where you came from.

... and what's their purpose/meaning?

Multible appearance of symbol parts delimited by double colons ::, indicates that the class or function declaration is embedded into a namespace or another class, e.g.:

namespace abc {
    namespace def {
        namespace ghk {
            class The_Class {
                void The_Function();
            };
        }
    }
}

or

class abc {
    class def {
        class ghk {
            class The_Class {
                void The_Function();
            };
        };
    };
};

or mixed

namespace abc {
    namespace def {
        class ghk { // No namespace definitions allowed inside.
            class The_Class {
                void The_Function();
            };
        };
    }
}

I think Java has the same feature for exactly the same purpose:
Distinguish class names from appearing in different namespaces / packages.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190