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

How to call a constructor from a constructor

Hi,

I have the following class. I want the constructor gnuplot_2d(int size)
has the same behavior as if I call from the 2-argument constructor
gnuplot_2d(size, size). But the following definition doesn't work. Do
you have any idea?

Although I can define
template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size) : _size_x(size),_size_y(size){
}
, it will be cumbersome if there are a lot of code in the function
body.

Best wishes,
Peng

template <typename __Tp>
class gnuplot_2d {
public:
gnuplot_2d(int size);
gnuplot_2d(int size_x, int size_y);
~gnuplot_2d(){};
private:
int _size_x;
int _size_y;
};

template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size) : gnuplot_2d<__Tp>(size, size){
}

template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size_x, int size_y) :
_size_x(size_x),_size_y(size_y){
}

Jul 23 '05 #1
9 3561
Pe*******@gmail.com wrote:
Hi,

I have the following class. I want the constructor gnuplot_2d(int size)
has the same behavior as if I call from the 2-argument constructor
gnuplot_2d(size, size). But the following definition doesn't work. Do
you have any idea?

Although I can define
template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size) : _size_x(size),_size_y(size){
}
, it will be cumbersome if there are a lot of code in the function
body.

Best wishes,
Peng

template <typename __Tp>
class gnuplot_2d {
public:
gnuplot_2d(int size);
gnuplot_2d(int size_x, int size_y);
~gnuplot_2d(){};
private:
int _size_x;
int _size_y;
};

template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size) : gnuplot_2d<__Tp>(size, size){
}

template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size_x, int size_y) :
_size_x(size_x),_size_y(size_y){
}


Why not just do the following, or is there more to the
problem than you have stated?

template <typename __Tp>
gnuplot_2d<__Tp>::gnuplot_2d(int size) :
_size_x(size), _size_y(size){
}
Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #2
As I said in the first message, suppose there are a long chunk of code
in the body of the definition of gnuplot_2d<__Tp>::gnuplot_2d(int
size_x, int size_y), you have to copy all the same code to the
definition of gnuplot_2d<__Tp>::gnuplot_2d(int size).

If you do that you create some problem to maintain the code. Suppose
later on you change the definition of gnuplot_2d(int size_x, int
size_y) a little bit, you have to change the definition of
nuplot_2d(int size) manually to make sure consistency.

I want a better solution to this problem. Thanks!

Best wishes,
Peng

Jul 23 '05 #3
Pe*******@gmail.com wrote:
As I said in the first message, suppose there are a long chunk of code
in the body of the definition of gnuplot_2d<__Tp>::gnuplot_2d(int
size_x, int size_y), you have to copy all the same code to the
definition of gnuplot_2d<__Tp>::gnuplot_2d(int size).

If you do that you create some problem to maintain the code. Suppose
later on you change the definition of gnuplot_2d(int size_x, int
size_y) a little bit, you have to change the definition of
nuplot_2d(int size) manually to make sure consistency.

I want a better solution to this problem. Thanks!

Best wishes,
Peng


If the constructors are complex, one approach is to factor
out the code common to all of the constructors (and often
the assignment operator) into a private function that each
of the constructors calls. Here's an incomplete example:

class A
{
public:
A() { doinit(0, 0); }
A(int x) { doinit(x, x); }
A(int x, int y) { doinit(x, y); }
private: // or protected
void doinit(int x, int y)
{
/* does complex stuff used by all constructors */
}
};

Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #4
So call a constructor from another constructor is impossible, right?

Thanks,
Peng

Jul 23 '05 #5
* Pe*******@gmail.com:

So call a constructor from another constructor is impossible, right?


The question seems to have some invalid assumptions, i.e., meaningless.

To call a constructor of class T on an object being constructed you'll have
to do use very dirty techniques and you'll be throwing overboard all the help
that the language normally gives you, just like when using a void* pointer;
it's not a good idea.

One clean solution to your problem is, instead,

template <typename Tp>
class gnuplot_2d_impl
{
gnuplot_2d_impl( int width, int height )
:
myWidth( width ), myHeight( myHeight )
{
// Lots of code here.
}
};

template <typename Tp>
class gnuplot_2d: private gnuplot_2d_impl
{
gnuplot_2d( int size ): gnuplot_2d_impl( size, size )
{}

gnuplot_2d( int width, int height ): gnuplot_2d_impl( width, height )
{}
};

Here the constructor calls are via the mechanism that C++ offers for calling
constructors from constructors: constructor initalizer lists.

Btw., note that two successive underscores are not allowed in C++ identifiers.

Neither is an underscore followed by an uppercase letter.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6
Pe*******@gmail.com wrote:
So call a constructor from another constructor is impossible, right?

You can't call constructors period. They are automatically invoked
for you as part of object creation. Other than the overloading choices
there isn't much you can do to affect their use.

As we've told you before, put the common code in a member function that
you CAN call.
Jul 23 '05 #7
> As I said in the first message, suppose there are a long chunk of code
in the body of the definition of gnuplot_2d<__Tp>::gnuplot_2d(int
size_x, int size_y), you have to copy all the same code to the
definition of gnuplot_2d<__Tp>::gnuplot_2d(int size).

If you do that you create some problem to maintain the code. Suppose
later on you change the definition of gnuplot_2d(int size_x, int
size_y) a little bit, you have to change the definition of
nuplot_2d(int size) manually to make sure consistency.

I want a better solution to this problem. Thanks!


You can use placement new. For example:

class N
{
public:
N(int a, int b) : x(a), y(b) {};
N(int c) { new (this) N(c, c); };
N() { new (this) N(0); };

int x, y;
};

vc7.1 optimises these equivalent to writing:

class N
{
public:
N(int a, int b) : x(a), y(b) {};
N(int c) { if(this) { x = c; y = c; }; };
N() { if(this) { x = 0; y = 0; }; };

int x, y;
};

You might be able to eliminate the if statments by writing your own
placement new operator.

Regards,
cadull
Jul 23 '05 #8
But, if you are using the above approach, i.e placement new, be sure to
call desturctor explicitely.

But here , there are two ways by which class can get generated, one
that uses placement new and one that does not. In one case u need to
call dtor explicitely and the other case you need not.

But, calling the dtor explicitely in both the cases may be harm less as
we really can not know (with out a ugly global/member varibale) how the
object got created at first place

Jul 23 '05 #9
<Pe*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
So call a constructor from another constructor is impossible, right?


Simple answer: right. It's impossible.

- JFA1
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S³awek | last post by:
Can one constructor of an object call another constructor of the same class? class foo { foo(float f, int i) // a "full" constructor { ... } foo(int i) // a "simplified" constructor {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Greg | last post by:
Is it possible to call a constructor from within a constructor I mean Class A { public A(string getModifiedVal) { .........
18
by: AlexanderVX | last post by:
How do I write a constructor mehtod call in this case /*-----------*/ template<typename Tclass CObjectPoolImpl { public: void smth(T* pObj) { if (pObj)
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
7
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
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? ...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.