Jump to content

  • Log In with Google      Sign In   
  • Create Account

Class with member that has constructor requiring a variable


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1244

Like
0Likes
Like

Posted 19 May 2013 - 10:26 AM

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.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2635

Like
1Likes
Like

Posted 19 May 2013 - 10:57 AM

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, 19 May 2013 - 10:59 AM.

o3o


#3 BaneTrapper   Members   -  Reputation: 1244

Like
0Likes
Like

Posted 20 May 2013 - 09:57 AM

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.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS