Constructor problem

E

Edith Gross

I'd like to have two constructors:

class X {
X(char *s);
X(float x);
...
};

Now when the second constructor is called, I want to convert the floating
point number into a string (that is simple) and call the first
contrsuctor. How can I do this? (X has some dynamically allocated memory.)

TIA,

EG
 
S

SirMike

Edith Gross on 2005-05-05 12:41 spammed:
Now when the second constructor is called, I want to convert the floating
point number into a string (that is simple) and call the first
contrsuctor. How can I do this? (X has some dynamically allocated memory.)
Use stringstream or sprintf.
like this:

char string1[50];
char string2[50];
short int var1;
float var2;
....
sprintf (string1, "%d", var1);
sprintf (string2, "%f", var2);
 
M

marbac

Edith said:
I'd like to have two constructors:

class X {
X(char *s);
X(float x);
...
};

Now when the second constructor is called, I want to convert the floating
point number into a string (that is simple) and call the first
contrsuctor. How can I do this? (X has some dynamically allocated memory.)

I don`t think that it is possible to call 2 constructors for one object.
One possible way would be to summarise the common code in an own
memberfunction and call it if required.
regards marbac
 
E

Edith Gross

Edith Gross on 2005-05-05 12:41 spammed:
Now when the second constructor is called, I want to convert the floating
point number into a string (that is simple) and call the first
contrsuctor. How can I do this? (X has some dynamically allocated memory.)
Use stringstream or sprintf.
like this:

char string1[50];
char string2[50];
short int var1;
float var2;
...
sprintf (string1, "%d", var1);
sprintf (string2, "%f", var2);

Thx, but you have misunderstood my problem. My problem is, how a
constructor for a class calls another constructor for the same class.

TIA,
EG
 
A

Alf P. Steinbach

* Edith Gross:
Thx, but you have misunderstood my problem. My problem is, how a
constructor for a class calls another constructor for the same class.

See <url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>.

In addition to the FAQ's solutions one (limited) solution is to
refactor the class in question as two or more classes with inheritance
relationship, or to refactor the class as one class with more data members,
i.e. moving the "called" constructor(s) to base classes and/or members.
 
S

SirMike

Edith Gross on 2005-05-05 13:00 spammed:
Thx, but you have misunderstood my problem. My problem is, how a
constructor for a class calls another constructor for the same class.
Oh, I am sorry but after FAQ reading everything should be clear...
 
R

Ron Natalie

Edith said:
I'd like to have two constructors:

class X {
X(char *s);
X(float x);
...
};

Now when the second constructor is called, I want to convert the floating
point number into a string (that is simple) and call the first
contrsuctor. How can I do this? (X has some dynamically allocated memory.)
You can't call constructors period.

Move the code you want to be common to both constructors into a separate
member function then you can call it.
 
C

codigo

Edith Gross said:
I'd like to have two constructors:

class X {
X(char *s);
X(float x);
...
};

Now when the second constructor is called, I want to convert the floating
point number into a string (that is simple) and call the first
contrsuctor. How can I do this? (X has some dynamically allocated memory.)

Constructors are not called, only invoked. Member functions are called.
Which is a good thing since you'ld have to delete twice to deallocate an
object that was constructed with that alternate constructor (and track which
object was allocated with what ctor).

Why not use a std::string instead of a pointer to char? You can provide a
universal converter template to convert the alternate ctor's parameter in
its initialization list.

// TtoStr.h
#include <sstream>

template<class T> std::string TtoStr(const T& r_t_)
{
std::eek:stringstream ossbuffer;
ossbuffer << r_t_;
return ossbuffer.str();
}

// X.h
#include <string>
#include "TtoStr.h"

class X
{
std::string m_s;
public:
X(std::string s) : m_s(s) { };
X(float n) : m_s(TtoStr(n)) { };
void display()
{
cout << m_s << std::endl;
}
};

// TestX.cpp
#include "X.h"

int main()
{
X* x = new X(10.67);
x->display();

delete x;

return 0;
}
 
M

Michael

or use Boost's lexical_cast<float>(str)
which i found the other day to my great joy, and want to spread the
greatness!!
:)
Mike
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top