Constructor problem

Discussion in 'C++' started by Edith Gross, May 5, 2005.

  1. Edith Gross

    Edith Gross Guest

    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 =----
     
    Edith Gross, May 5, 2005
    #1
    1. Advertising

  2. Edith Gross

    SirMike Guest

    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
     
    SirMike, May 5, 2005
    #2
    1. Advertising

  3. Edith Gross

    marbac Guest

    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
     
    marbac, May 5, 2005
    #3
  4. Edith Gross

    Edith Gross Guest

    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 =----
     
    Edith Gross, May 5, 2005
    #4
  5. * 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?
     
    Alf P. Steinbach, May 5, 2005
    #5
  6. Edith Gross

    SirMike Guest

    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
     
    SirMike, May 5, 2005
    #6
  7. Edith Gross

    Ron Natalie Guest

    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.
     
    Ron Natalie, May 5, 2005
    #7
  8. Edith Gross

    codigo Guest

    "Edith Gross" <> wrote in message
    news:p...
    > 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;
    }
     
    codigo, May 5, 2005
    #8
  9. Edith Gross

    Michael Guest

    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" <> wrote in message
    news:Jkree.6219$...
    >
    > "Edith Gross" <> wrote in message
    > news:p...
    > > 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;
    > }
    >
    >
     
    Michael, May 5, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,061
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,343
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,848
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    476
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,292
Loading...

Share This Page