Unknown syntax funtion():x,y

Started by
4 comments, last by Shakedown 16 years ago
Hi I found an interesting piece of code, but the code does not compile. and i dont know what this special syntax means. I dont know the keyword for google, so searching is quite hard.


class CException
{    
    friend class AutomaticGuardUnguard;

    public:
        CException(const std::string &Desc);
        std::string getCallStackString();
    private:
        static void _pushFunction(const char *FuncName);    
        static void _popFunction();

        std::string     m_ErrorDescription;
        unsigned int    m_StackDepth;
        
        static unsigned int                  ms_StackDepth;
        static std::vector<const char*>      ms_Vector;
};

unsigned int CException::ms_StackDepth = 0;
std::vector<const char*> CException::ms_Vector;

//------------------------------------------------------------------------

//THIS LINE OF CODE 
//constructor():datamember ???
CException::CException(const string &Desc) : m_ErrorDescription(Desc), m_StackDepth(ms_StackDepth)
{

}     
The declarition of CException. What does that : m_ErrorDescription(Desc), m_StackDepth(ms_StackDepth) mean? I know that those both are data member, but i dont know what this double point means in that context. The compiler complains about the return type of the ctr. (If i add anything, he complains, that ctrs must not have any return type).

1>Compiling...
1>main.cpp
1>c:\documents and settings\ben\desktop\prometha\header\exception.h(39) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\ben\desktop\prometha\header\exception.h(39) : error C2143: syntax error : missing ',' before '&'
1>c:\documents and settings\ben\desktop\prometha\header\exception.h(39) : error C2511: 'CException::CException(const int)' : overloaded member function not found in 'CException'
1>        c:\documents and settings\ben\desktop\prometha\header\exception.h(17) : see declaration of 'CException'
Advertisement
This is a constructor initialization list.
: m_ErrorDescription(Desc), m_StackDepth(ms_StackDepth)
That is an initializing list it sets those properties to the values inside the brackets. Its the similiar to having
CException::CException(const string &Desc)
{
m_ErrorDescription = Desc;
m_StackDepth = ms_StackDepth;
}
okay very good.
and why do i get that error msg from the compiler ?
missing type specifier, i mean, ctr do not have types.
okay i got it,

string --> std::string
i forget that namespace .. thank you very much
Using the constructor initialization list is slightly beneficial compared to setting the variables inside the constructor body. When you set the variables in the constructor body, the variables are first created, and upon this creation they're given a value which you immediately overwrite once the constructor body executes.

When you use the initialization list the variables are created with the given values, saving the step of creating them and then overwriting them.

This topic is closed to new replies.

Advertisement