calling default instead of argument constructor, why?!

This is a discussion on calling default instead of argument constructor, why?! within the C++ Programming forums, part of the General Programming Boards category; Code: class String { protected: enum {SZ = 80}; char str[SZ]; public: String() //this is being called from Pstring's else ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    5

    Question calling default instead of argument constructor, why?!

    Code:
    class String
    {
     protected:
      enum {SZ = 80};
      char str[SZ];
    
     public:
      String() //this is being called from Pstring's else
      {str[0] = '\0';}
    
      String(char s[])
      {strcpy(str, s);}
    
      void display() const
      {cout << str;}
    
      operator char*()
      {return str;}
    };
    
    class Pstring: public String
    {
     public:
      Pstring(char s[]);
    };
    
    Pstring::Pstring(char s[])
    {
     if (strlen(s) > SZ-1)
     {
      //do something
     }
    
     else String(s); // this is calling String() and not String(char s[])!!
    }
    
    int main()
    {
     Pstring test = "testing";
     test.display();
     return 0;
    }
    When I initialize test, it goes into Pstring's else statement which calls String(s), but instead of calling String(char s[]) it's calling the no argument constructor String() even though I'm sending it the argument (s)!! Why??

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class String
    {
    public:
       String()
       {
          cout << "String C1" << endl;
       }
    
       String(char s[])
       {
          cout << "String C2" << endl;
       }
    };
    
    class Pstring: public String
    {
    public:
       Pstring(char s[]) : String( s ) // <-- Your problem layed here
       {
          cout << "PSting C1" << endl;
       }
    };
    
    
    int main()
    {
       Pstring test = "testing";
       return 0;
    }

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    he didnt ask for a fix he asked why. you know if i could stay on id help him, but dude stop spoon feeding and teach someone.

    on second thought dont because you do everything backasswards anyway.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    Thanks, that fixed it, but how come?

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    stfu RoD...

    Because when you call any function from class PString the default constructor of the base class (class String) gets called. The default constructor is always String(), so you need to hardwire it so that when you call the PString(char s[])'s constructor, the constructor of the base class String(char s[]) gets called instead of the default String(). Does that make sense?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    I see, that makes sense, thanks

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Note that

    else String(s); // this is calling String() and not String(char s[])!!
    }

    doesn't call the constructor of the PString's base String class, it creates a temporary String on the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Peculiar malloc / command argument question
    By Beowolf in forum C Programming
    Replies: 4
    Last Post: 09-11-2007, 12:54 AM
  2. Replies: 26
    Last Post: 08-29-2007, 09:55 PM
  3. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 07:27 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. constructors
    By shrivk in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 10:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21