473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

constructor taking arguments

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
}
Jul 19 '05 #1
2 2897

"ishekar" <is******@vsnl.net> wrote in message news:bf**********@news.mch.sbs.de...
| 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
Jul 19 '05 #2
this works.

thanks

"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:bf************@ID-110726.news.uni-berlin.de...

"ishekar" <is******@vsnl.net> wrote in message news:bf**********@news.mch.sbs.de... | 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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
3
by: Alex | last post by:
Given this base class: class Base { Base(){} } Are these the same: class Test : Base
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
1
by: Bob Johnson | last post by:
Please note that this question is NOT about the merits of Microsoft certification (no need to get into that here). While taking a MS certification exam, I came across a question where it was...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.