constructor taking arguments

I

ishekar

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
}
 
C

Chris \( Val \)

| 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top