473,766 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a Overloaded constructor winthin a constructor

13 New Member
I m a beginner programmer and new to this site so i m not quite sure whether this is the correct place to ask this question.

I saw the 2 articles published here about this question but can't seem to understand it.

i have a class like this withe some overloaded constructors


class Abcd{
public: //sorry for forgetting this earlier
Abcd();
Abcd(int i);
}

Abcd::Abcd(int i){
............... ............... .......
..Big code here calculating
something using int i.......
............... ............... ............... ...
}

// Why won't This work?
Abcd::Abcd(){
Abcd::Abcd(10);
}



i m quite sure c++ supports such things like in java. and if it don't can someone plesae tell me a alternate way.

-----------------------------------------------------------------------------------------
ThankX everyone for the fast replies.
What I actually wanted to do was to cerate a class 'Encode' with two constructors
Encode(QCString * value){ .....big code in herer......}
Encode(const char* value){ Encode(new QCString(value) ); }
May 21 '07 #1
8 1958
weaknessforcats
9,208 Recognized Expert Moderator Expert
This call:
Expand|Select|Wrap|Line Numbers
  1. Abcd::Abcd(10);
  2.  
Calls the Abcd::Abcd(int) constructor. But no object is is specified. So, the compiler creates a temporary object as local to the Abcd::Abcd() function and initializes it with the 10. Unfortunately, this is NOT the object being initialized by the Abcd::Abcd() constructor. This temporary object will be destroyed at the end of the Abcd::Abcd() function.

If you want 10 as the default value, use the initializer list:
Expand|Select|Wrap|Line Numbers
  1. Abcd::Abcd() : datavariablename(10)
  2. {
  3. }
  4.  
May 21 '07 #2
DhananjayNangare
8 New Member
I m a beginner programmer and new to this site so i m not quite sure whether this is the correct place to ask this question.

I saw the 2 articles published here about this question but can't seem to understand it.

i have a class like this withe some overloaded constructors


class Abcd{
Abcd();
Abcd(int i);
}

Abcd::Abcd(int i){
............... ............... .......
..Big code here calculating
something using int i.......
............... ............... ............... ...
}

// Why won't This work?
Abcd::Abcd(){
Abcd::Abcd(10);
}



i m quite sure c++ supports such things like in java. and if it don't can someone plesae tell me a alternate way.
Hi,
I tried it this way on vc++ 6.0
Works fine
#include <iostream.h>

class myclass
{

public:

myclass()
{
cout << "\n Defualt";
myclass::myclas s(10);
}
myclass(int i)
{
cout <<"\n Overloaded :" <<i;
}
};
void main()
{
myclass m;
}

Output:

Defualt
Overloaded :10Press any key to continue
May 21 '07 #3
dcollins
4 New Member
What you've told the compiler is that there is a class named
Abcd that has two private constructors, one is a default
constructor (it takes no arguments) and one that takes an
int. Member functions and data are private unless
explicitly declared otherwise as "public" or "protected" . In
this case you want to declare at least one public
constructor so you can instantiate the object.

There isn't a need to call one constructor from the
other. Both functions create an object of type Abcd. The
correct constructor is selected by the compiler depending on
the arguments that are supplied when you create it.

The change to your class declaration and the syntax for
instantiating the object is shown below.

Expand|Select|Wrap|Line Numbers
  1. class Abcd {
  2. public:
  3.     Abcd( ) { };
  4.     Acbd( int i ) { /* some code */ };
  5. }; // <- Note the semi-colon
  6.  
  7.  
  8. // Instantiate an Abcd object
  9.  
  10.    Abcd abcd(10);
May 21 '07 #4
shanakard
13 New Member
Hi,
I tried it this way on vc++ 6.0
Works fine
#include <iostream.h>

class myclass
{

public:

myclass()
{
cout << "\n Defualt";
myclass::myclas s(10);
}
myclass(int i)
{
cout <<"\n Overloaded :" <<i;
}
};
void main()
{
myclass m;
}

Output:

Defualt
Overloaded :10Press any key to continue
that must be why i remember dowing this earlier. but it dosen't work in linux (g++)
May 21 '07 #5
shanakard
13 New Member
What you've told the compiler is that there is a class named
Abcd that has two private constructors, one is a default
constructor (it takes no arguments) and one that takes an
int. Member functions and data are private unless
explicitly declared otherwise as "public" or "protected" . In
this case you want to declare at least one public
constructor so you can instantiate the object.

There isn't a need to call one constructor from the
other. Both functions create an object of type Abcd. The
correct constructor is selected by the compiler depending on
the arguments that are supplied when you create it.

The change to your class declaration and the syntax for
instantiating the object is shown below.

Expand|Select|Wrap|Line Numbers
  1. class Abcd {
  2. public:
  3.     Abcd( ) { };
  4.     Acbd( int i ) { /* some code */ };
  5. }; // <- Note the semi-colon
  6.  
  7.  
  8. // Instantiate an Abcd object
  9.  
  10.    Abcd abcd(10);
U are write both the constructors are public, my mistake

ThankX for the fast reply.
What I actually wanted to do was to cerate a class 'Encode' with two constructors

Encode(QCString * value){ .....big code in herer......}

Encode(const char* value){ Encode(new QCString(value) ); }

some thing like that
May 21 '07 #6
dcollins
4 New Member
What I actually wanted to do was to cerate a class 'Encode' with two constructors

Encode(QCString * value){ .....big code in herer......}

Encode(const char* value){ Encode(new QCString(value) ); }

some thing like that
I can't answer this case specifically because I don't know what a
QCString is. In general a QCString* and a const char* point to
different, incompatible types, so you wouldn't be able to use the same
code for both. Only if QCString has public member functions or
operators that return a pointer to its "raw data", can you use the
same code. This pointer would also need to be either a const char* or
something that can be safely cast to one. If a QCString is designed to
be used then obtaining this pointer should be easy.

I'll define QCString as a std::string in the following example. You
get a const char* from a std::string from its data( ) member
function.

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream.h>
  3.  
  4.     // make a guess at what a QCString is
  5.     typedef std::string QCString;
  6.  
  7.     class Encode
  8.     {
  9.     public:
  10.         Encode( const QCString* value )
  11.         {
  12.             Encode( value->data( ) );
  13.         };
  14.         Encode( const char* value )
  15.         {
  16.             cout << "Some complex function of " << value << endl;
  17.         };
  18.     };
  19.  
  20. int main( int argc, char* argv[ ] )
  21. {
  22.     // Allocate a QCString on the heap
  23.     QCString* pQCString = new QCString( "A QCString." );
  24.  
  25.     // create a local Encode object, initialized from a constant QCString pointer
  26.     Encode encode_one( pQCString );
  27.  
  28.     // Deallocate the QCString to prevent memory leaks
  29.     delete pQCString;
  30.  
  31.     // create a local Encode object, initialized from a const char array
  32.     Encode encode_two( "A const char array." );
  33.  
  34.      return 0;
  35. }
May 21 '07 #7
shanakard
13 New Member
"weaknessforcat s" is right
calling a constructor from within a constructor only creates a temporary object

ive tried the following program and it can be confirmed
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream.h>
  3.  
  4. // make a guess at what a QCString is
  5. typedef std::string QCString;
  6.  
  7. class Encode
  8. {
  9. public:
  10.     int v;
  11.  
  12.     Encode( const QCString* value )
  13.     {    v=5;
  14.         cout << this << " Some complex function of " << *value << endl;
  15.     }
  16.     Encode( const char* value )
  17.     {    v=3;
  18.         Encode( new QCString(value) );
  19.         cout<<"ok "<<endl;
  20.     }
  21.     ~Encode()
  22.     {
  23.         cout<<"destroying"<<endl;
  24.     }
  25. };
  26.  
  27. int main( int argc, char* argv[ ] )
  28. {
  29.     // Allocate a QCString on the heap
  30.     QCString* pQCString = new QCString( "A QCString." );
  31.  
  32.     // create a local Encode object, initialized from a constant QCString pointer
  33.     Encode* i =new Encode( pQCString );
  34.  
  35.     cout<< i->v<<endl;
  36.     // Deallocate the QCString to prevent memory leaks
  37.     delete pQCString;
  38.     //delete i;
  39.  
  40.     // create a local Encode object, initialized from a const char array
  41.     i=new Encode( "A const char array." );
  42.  
  43.     cout<< i <<" new addr"<<endl;
  44.     cout<< i->v<<endl;
  45.  
  46.     return 0;
  47. }
  48.  
  49.  
May 23 '07 #8
shanakard
13 New Member
anyway BIG thankX for everyone who repiled :-)
May 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1825
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a memory problem that I'm not readily seeing that lies within the overloaded operator - I think pertaining to the temporary class object that is created and used to return a value of the operands and the pointer variable of the class. Could someone...
2
1688
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you can see in the constructor. Class Matris should create a matris by using class Intvektor. So what I want to have is a pointer to an array of Intvektor and in each positionindex in this array will I have a pointer to an array of Intvektor
6
2109
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
1
2689
by: R.Balaji | last post by:
Hi, How do I call the overloaded construction when I create the instance using the Reflection? for eg) namespace MySpace { Interface IOrganization
4
1486
by: Roger Webb | last post by:
How do you go about calling a constructor in the same class from another constructor? (see below) - Roger ie. public class1() { Does some general initialization
2
4000
by: Michael Wild | last post by:
hi everybody i suppose this question has been answered a countless times, but i can't seem to find the answer... how do i call an overloaded version of a constructor from a constructor? suppose something like this: class A {
2
7375
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
5
2293
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
6
4171
by: Joe HM | last post by:
Hello - I have a question regarding overloading the New() in a MustInherit Class. The following show the code in question ... MustInherit Class cAbstract Public MustOverride Sub print() Protected mName As String
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9837
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.