Thread: constructor argument confusion

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    40

    constructor argument confusion

    Hi everyone,
    I have a problem regarding class constructors, I know they are not like any other function in some ways and I was wondering if the following is one of them:

    Code:
    person (int age, float height, string* name)
    Why do I send strings and chars as pointers, while int float double etc by value?
    and, I have noticed that I manipulate them as pointers not as derefrenced pointers.
    as in:
    Code:
    person (int age, float height, string* name)
    {
         cout<<name<<endl;
    }


    while in normal functions

    Code:
    void swap (int*x, int*y)
    {
       int temp=*x;
       *x=*y;
       *y=temp;
      cout<<*x<<*y;
    }


    if any body can tell me whats the deal with that..........Thanks

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    When you pass by value, you create a copy of the object. After that, you work on the copy of that object in your function. Passing by pointer or reference, you will be working with the original object itself. So, built-in type like int, double, float, etc.. are small, it's ok to pass them by value. When you objects contains hundreds of members, passing by value becomes inefficient because you have to create copies of the objects, so we resort to passing by pointer or references.
    In your swap function, you want to work with the original objects instead of their copies so you pass by pointer.

    Code:
    void swap(int x, int y)
    If you write like above, x and y is just the copies of the values you pass in. In this case, they only have function scope, and are destroyed after the function returns.
    Last edited by nimitzhunter; 04-30-2011 at 01:10 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Blacky Ducky View Post
    Why do I send strings and chars as pointers, while int float double etc by value?
    I don't know, you tell us. Why on earth would you pass a std::string to a constructor by pointer? Seems like a very unusual thing to do to me. Typically one would either accept it by const-reference or one would instead accept a const pointer to char instead.

    This is very wrong if name is a pointer to string.
    Code:
    cout<<name<<endl;
    What makes you think that would work?

    There's nothing different with passing arguments to a constructor than to any other function.
    Last edited by iMalc; 04-30-2011 at 03:01 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default constructor == constructor without argument?
    By cyberfish in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2009, 03:25 AM
  2. argument 3 confusion in scandir()
    By DJTurboToJo in forum C Programming
    Replies: 2
    Last Post: 07-20-2007, 04:12 PM
  3. Variadic Argument in a Constructor
    By g4j31a5 in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2006, 10:22 PM
  4. Assignment operator in a constructor argument
    By chadwickstein in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2006, 04:08 PM
  5. two-argument constructor
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2002, 11:35 AM