473,385 Members | 1,673 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,385 software developers and data experts.

constructor overloading with static variable

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
7 4146
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
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
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
Unfortunately, someConst_ is an int... So can you please how to deal
with that too?

Apr 18 '06 #5
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
2
by: Sergey Krushinsky | last post by:
Hello all, Is there a common way to emulate constructor overloading in Python class? For instanse, I have 3 classes: 1/ Polar - to hold polar coordinates; 2/ Cartesian - to hold cartesian...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
5
by: Wilfried Mestdagh | last post by:
Hi, I want to overload a constructor of a class. But I want to call the other one from the second if called. I explain with code because of my english: public class XMLConfig { public...
14
by: Klaus Löffelmann | last post by:
Hi, does anybody know, why the second contructor isn't called in this example? Are you able to reproduce this bug? Thanks Klaus Public Class Something
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
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? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.