By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

constructor overloading with static variable

P: n/a
i have to overload a constructor that takes a constant static public
data member of a class... Can anybody tell me its syntax of prototype
and implementation?

Apr 18 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
coinjo wrote:
i have to overload a constructor that takes a constant static public
data member of a class... Can anybody tell me its syntax of prototype
and implementation?


First of all, eschew public data members.

Second, what do you mean? Does the constructor take a value that it
wants to stuff into its own constant static data member? (Answer: this
can't be done.) Does the constructor take a static constant from some
other class as a parameter? (Answer: just pass it in as a const or
const reference as usual.)

If that doesn't get at it, please rephrase.

Cheers! --M

Apr 18 '06 #2

P: n/a
The constructor takes a constant static data member of its own class
and stores it in one of its non constant and non static data member...
The problem that i am facing here that i am already using a constructor
that takes two int values... Now this constructor takes one int and one
constant static public data member of its own class... How to
differentiate it with the other? And can you please tell me the syntax
with an example...???

Apr 18 '06 #3

P: n/a
coinjo wrote:
The constructor takes a constant static data member of its own class
and stores it in one of its non constant and non static data member...
The problem that i am facing here that i am already using a constructor
that takes two int values... Now this constructor takes one int and one
constant static public data member of its own class... How to
differentiate it with the other? And can you please tell me the syntax
with an example...???


Well, if the data member in question is a non-int, it's simple:

struct A
{
A( int, int );
A( int, float );

static const float someConst_;

// ...
};

const float A::someConst_ = 3.14159;

// ...

void Foo()
{
A a1( 1, 2 ); // Calls first constructor
A a2( 1, A::someConst_ ); // Calls second constructor
// ...
}

That's just the usual function overloading.

If someConst_ were an int instead of a float, you'd need to play some
sort of trick (e.g., supplying a dummy parameter to the constructor,
wrapping the constant in a dummy type, etc.). Do you need help with
that?

Cheers! --M

Apr 18 '06 #4

P: n/a
Unfortunately, someConst_ is an int... So can you please how to deal
with that too?

Apr 18 '06 #5

P: n/a
coinjo wrote:
Unfortunately, someConst_ is an int... So can you please how to deal
with that too?


Merge the constructors that have the same signature into one. Or make
the second one to accept a pointer and pass the address of the static
data member. Or learn to use default argument values.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '06 #6

P: n/a
coinjo wrote:
Unfortunately, someConst_ is an int... So can you please how to deal
with that too?


Probably the best way to handle this case (especially if you have a
multiple constants) is to use an enumeration instead:

struct A
{
enum SomeEnum
{
ALPHA = 5,
BETA = 10,
GAMMA = 55
};

A( int, int );
A( int, SomeEnum );

// ...
};

void Foo()
{
A a1( 1, 5 ); // Calls first constructor
A a2( 1, A::BETA ); // Calls second constructor
// ...
}

Cheers! --M

Apr 18 '06 #7

P: n/a
> coinjo wrote:
Unfortunately, someConst_ is an int... So can you please how to deal
with that too?


PS, please quote the message you are responding to so that people not
using Google Groups can more easily follow the conversation. (To do so,
click "show options" and then "reply" in the revealed header.)

Cheers! --M

Apr 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.