Thread: c++ method declaration??

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question c++ method declaration??

    hi,

    I am trying to understand some sources and found the following:

    Code:
    // a task has name and time.
    struct task
    {
    	task(const string& inname, const LONGLONG intime): name(inname), time(intime) {}
    
    	string name;
    	LONGLONG time;
    };
    So I would like to know the difference between declaring task as above and
    Code:
    task(const string& inname, const LONGLONG intime){ name = &inname; time = intime; }
    Or is there any rule or convention I should follow?
    thanks in advance
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The first form is an initialiser list: name and time objects are constructed/initialised when the task object is created. name is constructed from inname using string's copy constructor, and time is created from intime. Then task's (empty) constructor body is executed.

    The second form (apart from a misplaced ampersand in the "name = inname;" statement) assigns values to name and time after they have been default constructed. name and intime are first default constructed (name created using string's default constructor, intime is created but uninitisalised). Then, within task's constructor body, name is assigned to the value of inname and time is assigned to the value of intime.

    As a rule of thumb, copy construction of an object is less expensive (in terms of CPU cycles, etc) than a default construction followed by a reassignment. This means that the initialiser list is usually the preferred method. However, keep in mind that I've used the term "rule of thumb" because there are real cases where such guidelines are inapplicable.

    Note: I have assumed LONGLONG is some basic type supported by the compiler, rather than a C++ class type. Basic types, when created, are unitialised.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    First one is constructor initializer list

    for this read this one out Initialization Lists in C++ - Cprogramming.com

    and the second one is copying data in constructor with assignment operator

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Smile thanks

    Thanks!
    I got it,
    Is just I have a question.
    you said
    copy construction of an object is less expensive (in terms of CPU cycles, etc) than a default construction followed by a reassignment.
    Why is that? I am not an expert in c++ but in other language I know copy constructor would imply allocating enough memory, then copy bytes into that memory and in the case of a default assign contructor It will mean just passing a pointer to the prior object.
    Isn't this the case of c++?
    Mac OS 10.6 Snow Leopard : Darwin

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    thanks.

    I got it right this time.
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread