Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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: 1376

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.



Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 3497

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: 1376

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.






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