Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Member initalization list. Training and... got stuck

This topic is 2021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.


Supose i  have a really basic class that has two member variables.

one of member variables takes two paramethers, one is a member variable of current class and second is a variable passed intro the constructor.

The problem is that i know how to pass the variable from class constructor to member variable but how do i pass the member variable to member variable?

 

 

class Game

{

public:

    Game(EventHandle &objEventHandle) : objMap(objEventHandle){}



    Camera objCam();

    Map objMap(objCam, objEventHandle);//This var... how to pass the objCam here that is gonna be constructed in this class?

private:

}
Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Advertisement

Pass the member as a parameter to the member constructor like any other parameter.

class Game

{

public:

    Game(EventHandle &objEventHandle) : objMap(objCam, objEventHandle) {}

    Camera objCam;
    Map objMap;
}

Share this post


Link to post
Share on other sites

Why would you?  Your method already has access to objCam because its in the same class.

Share this post


Link to post
Share on other sites

Function, Varaible ... they are not the same thing, you are getting confused between the two.

 

 

A Varaible holds data, It can either be a primitive type, String, char int, float double or a custom data type (Class)

It is declared as such:

[Access Modifier] [MemberType] [MemberName];
 
E.g.
public int MyAge;
 
or
public:
      int MyAge;

 

A Function is a set of instruction which is called via an object. It takes parameters and has a return type, and is declared as such

[Access modifier] [return type] [functionName]([Paramters])
{
    return [return value]; //only required if return type is not void
}
 
public int ReturnAge() { return MyAge; }

Share this post


Link to post
Share on other sites

Why would you?  Your method already has access to objCam because its in the same class.

Its a class, and it has only one constructor, in order to create a object of it, it requires 2 parameters i need to fill up.

Would that answer your question?

 

Why did someone make it like that? i suppose i could ask...

Share this post


Link to post
Share on other sites

Pass the member as a parameter to the member constructor like any other parameter.

 

class Game

{

public:

    Game(EventHandle &objEventHandle) : objMap(objCam, objEventHandle) {}

    Camera objCam;
    Map objMap;
}

That would do the job.

But i was unable to get a consturctor to be defined at cpp

 

What is the problem now, i dont know the syntax to declare at class Three constructor definition without the body (i just want the declaration and definition to be at other place) "Three(int a) : objTwo(a, objOne){}"

Anything i tried produces me a syntax error "expected "{" before ...(at begging of next line after constructor definition)"

 

///////////////
// One
///////////////
class One
{ public:    One(); };
One::One(){}
//////////
// Two parameter
//////////
class Two
{ public: Two(int i, One objOne); };
Two::Two(int i, One objOne){}
//////////////
//Class Three
//////////////
class Three
{
public:
    Three(int a) : objTwo(a, objOne){} //If i switch "{}" with ";" i get syntax error... What em i supposed to write so its only definition?
    One objOne;
    Two objTwo;
};
Three::Three(int a) // I want the constructor definition to be here
{
    //Some code
}
////////
// main
////////
int main()
{
    Three(1);
    return 0;
}
Edited by BaneTrapper

Share this post


Link to post
Share on other sites

class Three
{
public:
    Three(int a) : objTwo(a, objOne){} //If i switch "{}" with ";" i get syntax error... What em i supposed to write so its only definition?
    One objOne;
    Two objTwo;
};
Three::Three(int a) // I want the constructor definition to be here
{
    //Some code
}

The initializer list is a part of the definition, not the declaration.

class Three
{
public:
    Three(int a);
    One objOne;
    Two objTwo;
};
Three::Three(int a) : objTwo(a, objOne)
{
    //Some code
}

Share this post


Link to post
Share on other sites

objOne won't have been constructed when the Three constructor is called. I'm pretty sure that the only thing that is safe to do with objOne in the base class constructor is take the address... could be wrong though...

Share this post


Link to post
Share on other sites

 

class Three
{
public:
    Three(int a) : objTwo(a, objOne){} //If i switch "{}" with ";" i get syntax error... What em i supposed to write so its only definition?
    One objOne;
    Two objTwo;
};
Three::Three(int a) // I want the constructor definition to be here
{
    //Some code
}

The initializer list is a part of the definition, not the declaration.

 

class Three
{
public:
    Three(int a);
    One objOne;
    Two objTwo;
};
Three::Three(int a) : objTwo(a, objOne)
{
    //Some code
}

:( :( ... i tried so many wais...

I just... i sould have tried / thought that... i failed.

Thank you allot on helping!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!