Thread: classes and overloading constructors

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    classes and overloading constructors

    hi,

    i have a class with the data types i need.

    so far i've implemented the = operator which takes the items in the right side and sets it in the left side.

    so in the main if i said:
    CD1 = CD2;
    then all the members of CD2 are copied to CD1. i think it's called a copy constructor.

    however, now my question pertains to another constructor.

    if i want to initialize an integer and set it equal to 5, then i can say:
    int var = 5;
    int var("5");

    how can i do this? what type of operator do i have to overload if that's what i have to do?

    thanks,
    barneygumble742

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You learned copy constructors first? Pretty wierd. Well, initializations like int var(5) is the constructor type that I learned first. Here's a few examples of different constructors.

    Code:
    class Foo
    {
    public:
    	Foo() : iVar(0) { std::cout << iVar; }
    	Foo(const int a) : iVar(a) { std::cout << iVar; }
    	Foo(const char* a) : iVar(atoi(a)) { std::cout << iVar; }
    	Foo(const Foo& a) : iVar(a.iVar) { std::cout << iVar; }
    
    	
    private:
    	const int iVar;
    };
    
    
    int main(int argc, char *argv[])
    {
    	Foo fooa;        // Foo()
    	Foo foob(5);     // Foo(const int a)
    	Foo fooc("6");   // Foo(const char* a)
    	Foo food = fooa; // Foo(const Foo& a)
    	return 0;
    }
    I don't know if you were aware but saying Foo fooc("6"); passes a char*, double quoted items are treated as strings so I used atoi in the Foo(const char* a). I don't even know if I addressed your question correctly. No operator overloading, but constructor overloading here.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Be carefull, this demonstrates the copy constructor:
    Code:
    TypeX CD2;
    TypeX CD1 = CD2;  // Copy constructor called here
    This however is the assignment operator:
    Code:
    TypeX CD2;
    TypeX CD1;
    CD1 = CD2;  // Assignment operator called here
    This is because the first example above gets translated into this:
    Code:
    TypeX CD2;
    TypeX CD1(CD2);  // Copy constructor called
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  4. Constructor Overloading
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2005, 05:11 AM
  5. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM