Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Class with member that has constructor requiring a variable

This topic is 1950 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.

 

Ive bean digging true "c++ initialization list" and i didnt find what i was looking for.

 

Question explained with words:

Is there a way for a class that has a member that requires a variable passed for consturcor take a variable from the classes constructor instead of pasing alredy preset value?.

 

Q. Explained with code:

//Class one

class One

{

public:

    One(int a)

    {

        a = number;

    }

    int number;

};



//Class Two

class Two

{

public:

    Two(int a);//Two class takes a variable "int a" is there a way to pass this value for

    One objOne(a); //This member? and whats its called or smt.

};



//main

int main()

{

    Two objTwo(10);

    return 0;

}

 

Infront, thank you on your visit.

Share this post


Link to post
Share on other sites
Advertisement
class Meh{public:    Meh(int pass) : two(pass)    {}private:    Two two;};


Something like this?

EDIT:
If you calculate the value you want to pass in the constructor of the containing class, im not sure if its possible to pass it. You can always initialize it with a default value and immediately reassign it with a new one with your calculated value. Edited by Waterlimon

Share this post


Link to post
Share on other sites

class Meh{public:    Meh(int pass) : two(pass)    {}private:    Two two;};


Something like this?

EDIT:
If you calculate the value you want to pass in the constructor of the containing class, im not sure if its possible to pass it. You can always initialize it with a default value and immediately reassign it with a new one with your calculated value.

I will have to try it out,

 

Best way to explain it:
Imagine there was a int that would have a constructor with arguments.

i would declare int in the body of some class and i would like to pass him some arguments passed to the class it self.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!