Skip to main content
gamedev.net
🔒 Locked

Calling OTHER constructer...

Started by Erzengeldeslichtes 5 replies 650+ views
Original Post
Erzengeldeslichtes
Erzengeldeslichtes
Using: Microsoft Visual C++ 6.0 Setup: I have two constructors. One is protected and has 2 strings. The other is public and has only 1 string. Problem: They both do pretty much the exact same thing. The 1 string variaty does the same thing as the two string variaty, except it substitutes a class specific string for where the second string is in the other constructer. Sounds like a great place to use default values right? Nope, no can do. If I combine them into one, that means it's going to have to go into public. If it's in public, then someone can override the second string, and the point of the string is to pass class specific information down the hierarchy. What I want is for the public constructer to, without initializing anything, pass over to the protected constructer for initialization with a default string. I somehow doubt it's possible, but I'd like to know.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
TangentZ
TangentZ
If they both do pretty much the same things, then you
can move those same things into a private function and
have both constructors call that helper.

It's just basic refactoring, really.
神はサイコロを振らない!
Houdini
Houdini
TangentZ is right, you should use an init function and call it from your constructors. The C++ FAQ Lite explains why you can't call a constructor from another constructor.


- Houdini
Houdini
Erzengeldeslichtes
Erzengeldeslichtes
Yeah, that's what I do with things inside the constructer functions themselves, but there's between some and a lot of initialization (depending on the class and it's number of variables) that can't be shoved over to another function, and again they're both exactly the same except the string that needs to be default for the object being created...
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
TangentZ
TangentZ
Show some code... I find it very hard to understand what
you're trying to do...
神はサイコロを振らない!
Erzengeldeslichtes
Erzengeldeslichtes
class Base{protected:string m_INIFile;string m_INIHeader;Base(const E_Parameters& Overrides, const string& ActualClass){string CurParam(Overrides.GetValue("ini file"));if(CurParam != NULLSTR){    m_INIFile = StrProcessQuotes(CurParam);}if(!g_INIManager->LoadFile(m_INIFile)){    m_INIFile = FindDefaultINIFileForClass(ActualClass);    if(!g_INIManager->LoadFile(m_INIFile))    {        m_INIFile = NULLSTR;        return; //Leave everything at it's initialized default.    }}CurParam = Overrides.GetValue("ini header"));if(CurParam != NULLSTR){    m_INIHeader = StrProcessQuotes(CurParam);}if(!g_INIManager->HeaderExists(m_INIFile, m_INIHeader)){    m_INIHeader = FindDefaultINIHeaderForClass(ActualClass);    if(!g_INIManager->HeaderExists(m_INIFile, m_INIHeader))    {        m_INIFile = NULLSTR;        m_INIHeader = NULLSTR;        return;    }}}};class Deriv1 : public Base{protected:VECTOR Postion;VECTOR Momentum;/* AssignValues(Overrides, ActualClass)Checks to see if a parameter exists in overrides, assigns it if so.Otherwise checks the ini file if there is one, and if the value exists.If it's not there leaves it where it's initialized.*/void AssignValues(const E_Parameters& Overrides, const string& ActualClass);Deriv1(const E_Parameters& Overrides, const string& ActualClass): Position(0,0,0),Momentum(0,0,0),Base(Overrides, ActualClass){    AssignValues(Overrids, ActualClass);}public:Deriv1(const E_Parameters& Overrides): Position(0,0,0),Momentum(0,0,0),Base(Overrides, "Derv1"){    AssignValues(Overrides, "Derv1");}};

Keep in mind that this was heavily simplified. Use your imagination to expand it to something useful. In full it takes up many pages.

You can see in Derv 1 there's some initialization that was copy and pasted. According to C++ FAQ Lite, there's nothing I can do except copy and paste them, but in some of these objects the initialization list alone can be measured in screen lengths.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
TangentZ
TangentZ
Quote:
Original post by Erzengeldeslichtes
You can see in Derv 1 there's some initialization that was copy and pasted. According to C++ FAQ Lite, there's nothing I can do except copy and paste them, but in some of these objects the initialization list alone can be measured in screen lengths.


Ah, I see it now. You're specifically referring to the
initializer list for each constructor.

What the C++ FAQ Lite said. There is not much you can do about
the copy & pasting, other than leaving them out and let the
member variables be default-constructed.

In other words, constructors cannot share the
initializer list, at least not in the way you're implying.
神はサイコロを振らない!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.