constructor overloading with static variable

C

coinjo

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?
 
M

mlimber

coinjo said:
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
 
C

coinjo

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...???
 
M

mlimber

coinjo said:
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
 
V

Victor Bazarov

coinjo said:
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
 
M

mlimber

coinjo said:
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
 
M

mlimber

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
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top