Click here to Skip to main content
15,891,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir.

i create a class which containing one default and one parameter constructor..


C#
class cSimple
{
private:
    int icount;
    int iLoopValue;
public:                 //always constructure must be in public
        cSimple()
        {
            icount=0;
        }
        cSimple(int iTemp)
        {
            iLoopValue=iTemp;
        }
        void MyFunction()
        {
            for(;icount<iLoopValue;icount++)
                cout<<"The count value is :"<<icount<<endl;
        }
};



my doubt :


can i create a one single object to make a call to both the constructor..
if i done like that means it not running properly .. can you give the solution/


if it possible to call both the constructor with using single object..
Posted
Updated 19-Jul-11 3:05am
v2

cSimple()
{
    if(CommonInit() != 0){error handling}
}
cSimple(int iTemp)
{
   if(CommonInit() != 0){error handling}
   iLoopValue=iTemp;
}
int CSimple::CommonInit()
{
  icount = 0;
  iLoopValue = 0; //Don't leave any members uninitialized

  return 0;
}
 
Share this answer
 
Comments
Stefan_Lang 20-Jul-11 5:55am    
My 5 as it's the best (and pretty much only sensible) answer I can think of.
you cant call MyFunction() inside the constructor, but this is dangerous if severe errors are happening or no error handling possible


best is default arg:

cSimple(int iTemp = 0)
 
Share this answer
 
Comments
Dhanasundhari 19-Jul-11 9:03am    
sir,, it will execute only the parameter constructor ...

i want execute the default constructor too..
what i want to do..
KarstenK 20-Jul-11 7:12am    
What i wrote is defaulting arguments.
Do you this syntax?

cSimple() : icount(0)
{
}


Take my three experieneced advises to your heart:

1. dont run functions in constructors
2. read a basic C++ progemming book like "C++ for dummies"
3. go trouble out of the way so you win without fighting

Stefan_Lang 20-Jul-11 5:56am    
There's nothing wrong with calling other member functions in the constructor as long as they're not virtual.
KarstenK 20-Jul-11 7:15am    
timing and error handling are bad in constructor. Or would you fetch data from a databse oder search for an update in a constructor?

If you say "YES" THAN "There's nothing wrong with calling other member functions in the constructor as long as they're not virtual."
ELSE "but this is dangerous" :-O
Albert Holguin 20-Jul-11 10:14am    
I wouldn't say its dangerous at all... specially in the case of doing initialization... and no, you shouldn't access a database in a constructor...
You can call a constructor explicitly for an already created object:

cSimple myS; // Normal constructor call, creates the object

myS.cSimple::cSimple(134); // Explicit constuctor call on an existing object


This way you can call several constructors on the same object. This not a recommended practice.
 
Share this answer
 
v5
Comments
Stefan_Lang 20-Jul-11 5:53am    
My 1, because what you intended is not guaranteed to work, and might even crash the program.

Besides the call is syntactically incorrect and shouldn't* even compile. You are calling the constructor as a static function! The call should be:

myS.CSimple(134);

* P.s.: I would have said it *will* not, but some compilers do accept weird code occasionally...
YvesDaoust 20-Jul-11 11:01am    
Then the 1 goes to Visual C++ 2003.

The scoped version does compile and run. The straight version does not compile. (error C2274: 'function-style cast' : illegal as right side of '.' operator)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900