passing parameter in member declaration

Discussion in 'C++' started by =?ISO-8859-15?Q?Sven_K=F6hler?=, May 14, 2007.

  1. Hi,

    look at this example:

    struct T1 {
    bool v;

    T1(bool p = false) {
    this->v = p;
    }
    };

    struct T2 {
    T1 v1;
    T1 v2(true);
    };


    The declaration of v1 is OK.
    The declaration of v2 is not. I'd like to pass the parameter to the
    construtor, but i can't imagine how.

    How can i pass true to the constructor?
    Is it possible at all?


    Regards,
    Sven
     
    =?ISO-8859-15?Q?Sven_K=F6hler?=, May 14, 2007
    #1
    1. Advertising

  2. On Mon, 2007-05-14 at 03:16 +0200, Sven Köhler wrote:

    > struct T1 {
    > bool v;
    >
    > T1(bool p = false) {
    > this->v = p;
    > }
    > };
    >
    > struct T2 {
    > T1 v1;
    > T1 v2(true);
    > };
    >
    >
    > The declaration of v1 is OK.
    > The declaration of v2 is not. I'd like to pass the parameter to the
    > construtor, but i can't imagine how.
    >
    > How can i pass true to the constructor?
    > Is it possible at all?


    If I understand your question correctly, all you have to do is write a
    constructor for T2 and initialize its 'v2' member in the initialization
    list:

    T2::T2() :
    v2(true) {
    }

    I hope that helps,

    --
    Ney André de Mello Zunino
    http://zunino.eti.br/
     
    Ney =?ISO-8859-1?Q?Andr=E9?= de Mello Zunino, May 14, 2007
    #2
    1. Advertising

  3. On Mon, 14 May 2007 03:16:11 +0200, Sven Köhler wrote:

    > Hi,
    >
    > look at this example:
    >
    > struct T1 {
    > bool v;
    >
    > T1(bool p = false) {
    > this->v = p;
    > }
    > };
    >
    > struct T2 {
    > T1 v1;
    > T1 v2(true);
    > };
    >
    >
    > The declaration of v1 is OK.
    > The declaration of v2 is not. I'd like to pass the parameter to the
    > construtor, but i can't imagine how.
    >
    > How can i pass true to the constructor? Is it possible at all?


    Yes, you just need to do it in T2's constructor like so:

    struct T2 {
    T2() : v2(true) {}
    T1 v1;
    T1 v2;
    };

    --
    Markus Schoder
     
    Markus Schoder, May 14, 2007
    #3
  4. =?ISO-8859-15?Q?Sven_K=F6hler?=

    fr3@K Guest

    On May 14, 9:16 am, Sven Köhler <>
    wrote:
    > Hi,
    >
    > look at this example:
    >
    > struct T1 {
    > bool v;
    >
    > T1(bool p = false) {
    > this->v = p;
    > }
    >
    > };
    >
    > struct T2 {
    > T1 v1;
    > T1 v2(true);
    >
    > };
    >
    > The declaration of v1 is OK.
    > The declaration of v2 is not. I'd like to pass the parameter to the
    > construtor, but i can't imagine how.
    >
    > How can i pass true to the constructor?
    > Is it possible at all?
    >
    > Regards,
    > Sven


    Do that in T2's ctor:

    struct T2 {
    T1 v1;
    T1 v2;

    T2() : v2(true) {}
    };
     
    fr3@K, May 14, 2007
    #4
  5. > Yes, you just need to do it in T2's constructor like so:
    >
    > struct T2 {
    > T2() : v2(true) {}
    > T1 v1;
    > T1 v2;
    > };


    Exactly what i was looking for

    Thanks!
     
    =?UTF-8?B?U3ZlbiBLw7ZobGVy?=, May 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    643
    John Carson
    Jul 30, 2003
  2. Azdo
    Replies:
    2
    Views:
    450
  3. Stuart Redmann
    Replies:
    5
    Views:
    506
    Stuart Redmann
    Dec 14, 2007
  4. Luca Forlizzi
    Replies:
    4
    Views:
    480
    Luca Forlizzi
    Nov 14, 2010
  5. Skybuck Flying
    Replies:
    2
    Views:
    365
    Alf P. Steinbach /Usenet
    Jun 21, 2011
Loading...

Share This Page