Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constructor taking arguments

Reply
Thread Tools

constructor taking arguments

 
 
ishekar
Guest
Posts: n/a
 
      07-21-2003
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
}


 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      07-21-2003

"ishekar" <(E-Mail Removed)> wrote in message news:bfg2a2$2tp$(E-Mail Removed)...
| 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


 
Reply With Quote
 
 
 
 
ishekar
Guest
Posts: n/a
 
      07-21-2003
this works.

thanks

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

news:bfg2a2$2tp$(E-Mail Removed)...
> | 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
term does not evaluate to a function taking 1 arguments KevinSimonson C++ 6 10-23-2010 08:45 AM
taking arrays/containers as member function arguments of an API Hicham Mouline C++ 7 01-06-2009 01:16 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Functions taking pointers to different types as arguments Mikhail Teterin C Programming 9 12-24-2004 07:39 AM
Difference in module_eval taking block vs. taking string (1.8 bug?) Jim Cain Ruby 1 07-18-2003 02:01 AM



Advertisments