Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Constructor problem (http://www.velocityreviews.com/forums/t290819-constructor-problem.html)

Edith Gross 05-05-2005 10:41 AM

Constructor problem
 
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


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

SirMike 05-05-2005 10:52 AM

Re: Constructor problem
 
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);


--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com

marbac 05-05-2005 10:59 AM

Re: Constructor problem
 
Edith Gross wrote:
> 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

Edith Gross 05-05-2005 11:00 AM

Re: Constructor problem
 
On Thu, 05 May 2005 12:52:06 +0200, SirMike wrote:

> 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

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Alf P. Steinbach 05-05-2005 11:06 AM

Re: Constructor problem
 
* 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.

--
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?

SirMike 05-05-2005 11:14 AM

Re: Constructor problem
 
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...

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com

Ron Natalie 05-05-2005 12:03 PM

Re: Constructor problem
 
Edith Gross wrote:
> 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.

codigo 05-05-2005 03:56 PM

Re: Constructor problem
 

"Edith Gross" <egrossde@yahoo.de> wrote in message
news:pan.2005.05.05.10.41.23.812000@yahoo.de...
> 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::ostringstream 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;
}



Michael 05-05-2005 08:13 PM

Re: Constructor problem
 
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

"codigo" <codigo@codigo.trap.com> wrote in message
news:Jkree.6219$VL3.623064@news20.bellglobal.com.. .
>
> "Edith Gross" <egrossde@yahoo.de> wrote in message
> news:pan.2005.05.05.10.41.23.812000@yahoo.de...
> > 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::ostringstream 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;
> }
>
>





All times are GMT. The time now is 12:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.