constructor taking arguments

Discussion in 'C++' started by ishekar, Jul 21, 2003.

  1. ishekar

    ishekar Guest

    Hi,

    I have a class which has only one constructor that takes an argument.
    I want the users of this class to pass the parameter in the declaration.
    The problem i am facing is this cannot be used in the declaration section.
    is there any work around.

    thanks
    ishekar.

    class A
    {
    public:
    A(string c){...};
    }

    //some user using this class
    class B
    {
    public:
    B();
    private:
    A("This is B's copy of A"); // this is not permitted
    }
     
    ishekar, Jul 21, 2003
    #1
    1. Advertising

  2. "ishekar" <> wrote in message news:bfg2a2$2tp$...
    | Hi,
    |
    | I have a class which has only one constructor that takes an argument.
    | I want the users of this class to pass the parameter in the declaration.
    | The problem i am facing is this cannot be used in the declaration section.
    | is there any work around.

    [snip]

    Yes, use an initialiser list:

    class A
    {
    public:
    A( std::string s ) : S( s ) {};
    private:
    std::string S;
    };

    class B
    {
    public:
    B( const std::string& S ) : AObj( S ) {}
    private:
    A AObj;
    };

    int main()
    {
    B BObj( "This is B's copy of A" );

    return 0;
    }

    Note: Although I have modified B's constructor, you
    need not have done it this way.

    Cheers.
    Chris Val
     
    Chris \( Val \), Jul 21, 2003
    #2
    1. Advertising

  3. ishekar

    ishekar Guest

    this works.

    thanks

    "Chris ( Val )" <> wrote in message
    news:bfgfe7$egg3k$-berlin.de...
    >
    > "ishekar" <> wrote in message

    news:bfg2a2$2tp$...
    > | Hi,
    > |
    > | I have a class which has only one constructor that takes an argument.
    > | I want the users of this class to pass the parameter in the declaration.
    > | The problem i am facing is this cannot be used in the declaration

    section.
    > | is there any work around.
    >
    > [snip]
    >
    > Yes, use an initialiser list:
    >
    > class A
    > {
    > public:
    > A( std::string s ) : S( s ) {};
    > private:
    > std::string S;
    > };
    >
    > class B
    > {
    > public:
    > B( const std::string& S ) : AObj( S ) {}
    > private:
    > A AObj;
    > };
    >
    > int main()
    > {
    > B BObj( "This is B's copy of A" );
    >
    > return 0;
    > }
    >
    > Note: Although I have modified B's constructor, you
    > need not have done it this way.
    >
    > Cheers.
    > Chris Val
    >
    >
     
    ishekar, Jul 21, 2003
    #3
    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. Mikhail Teterin
    Replies:
    9
    Views:
    362
    Richard Bos
    Dec 24, 2004
  2. Generic Usenet Account
    Replies:
    10
    Views:
    2,245
  3. Hicham Mouline
    Replies:
    7
    Views:
    348
    Hicham Mouline
    Jan 6, 2009
  4. KevinSimonson
    Replies:
    6
    Views:
    842
    Bo Persson
    Oct 23, 2010
  5. Jim Cain
    Replies:
    1
    Views:
    209
    Yukihiro Matsumoto
    Jul 18, 2003
Loading...

Share This Page