Connecting Tech Pros Worldwide Help | Site Map

Constructor problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 03:58 AM
Edith Gross
Guest
 
Posts: n/a
Default 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 =----

  #2  
Old July 23rd, 2005, 03:58 AM
SirMike
Guest
 
Posts: n/a
Default Re: Constructor problem

Edith Gross on 2005-05-05 12:41 spammed:[color=blue]
> 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.)[/color]
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
  #3  
Old July 23rd, 2005, 03:58 AM
marbac
Guest
 
Posts: n/a
Default Re: Constructor problem

Edith Gross wrote:[color=blue]
> 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.)[/color]

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
  #4  
Old July 23rd, 2005, 03:58 AM
Edith Gross
Guest
 
Posts: n/a
Default Re: Constructor problem

On Thu, 05 May 2005 12:52:06 +0200, SirMike wrote:
[color=blue]
> Edith Gross on 2005-05-05 12:41 spammed:[color=green]
>> 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.)[/color]
> 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);[/color]

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 =----
  #5  
Old July 23rd, 2005, 03:58 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Constructor problem

* Edith Gross:[color=blue]
>
> Thx, but you have misunderstood my problem. My problem is, how a
> constructor for a class calls another constructor for the same class.[/color]

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?
  #6  
Old July 23rd, 2005, 03:59 AM
SirMike
Guest
 
Posts: n/a
Default Re: Constructor problem

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

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
  #7  
Old July 23rd, 2005, 03:59 AM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Constructor problem

Edith Gross wrote:[color=blue]
> 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.)
>[/color]
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.
  #8  
Old July 23rd, 2005, 03:59 AM
codigo
Guest
 
Posts: n/a
Default Re: Constructor problem


"Edith Gross" <egrossde@yahoo.de> wrote in message
news:pan.2005.05.05.10.41.23.812000@yahoo.de...[color=blue]
> 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.)
>[/color]

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;
}


  #9  
Old July 23rd, 2005, 04:00 AM
Michael
Guest
 
Posts: n/a
Default 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.. .[color=blue]
>
> "Edith Gross" <egrossde@yahoo.de> wrote in message
> news:pan.2005.05.05.10.41.23.812000@yahoo.de...[color=green]
> > 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[/color][/color]
floating[color=blue][color=green]
> > point number into a string (that is simple) and call the first
> > contrsuctor. How can I do this? (X has some dynamically allocated[/color][/color]
memory.)[color=blue][color=green]
> >[/color]
>
> 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[/color]
which[color=blue]
> 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;
> }
>
>[/color]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.