473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Initializing class object as member

Consider the following program:

#include <iostream>
#include <string>

class Test {
public:
Test(const std::string &val);

private:
std::string str;
};

Test::Test(const std::string &val) : str(val)
{
// str = val;
std::cout << "from one arg ctor of Test class: " << str << '\n';
return;
}

int main(void)
{
Test tmp("tmp object");

return 0;
}

This program's output is

from one arg ctor of Test class: tmp object

which is expected. In the ctor, Test::Test(const std::string &val) :
str(val),
instead of using str(val), if I uncomment str = val, then also the same
output is printed. Among these two ways of initializing str, which is
preferable and why ?

Jan 6 '07 #1
1 1436

subramanian napsal:
Consider the following program:

#include <iostream>
#include <string>

class Test {
public:
Test(const std::string &val);

private:
std::string str;
};

Test::Test(const std::string &val) : str(val)
{
// str = val;
std::cout << "from one arg ctor of Test class: " << str << '\n';
return;
}

int main(void)
{
Test tmp("tmp object");

return 0;
}

This program's output is

from one arg ctor of Test class: tmp object

which is expected. In the ctor, Test::Test(const std::string &val) :
str(val),
instead of using str(val), if I uncomment str = val, then also the same
output is printed. Among these two ways of initializing str, which is
preferable and why ?
Preffered form is

Constructor()
: member(prm1, prm2, ...)
{
}

because it simply calls constructor of member.

In form

Constructor()
{
member = ...;
}

Is called default constructor (without parameters) first and then
assignment operator. For built-in types it may be optimized, but for
class instances it works described way - it may not be optimized,
because there is no guarantee, that result of sequence {default
constructor, assignment operator} is the same as result of non-default
constructor (although in well designed code it should be so).

You can see this behaviour on following sample program:
#include <iostream>

class TestClass
{
public:
TestClass()
{
std::cout << "TestClass()\n";
}

TestClass(int data)
: data_(data)
{
std::cout << "TestClass(int)\n";
}

TestClass(const TestClass& tc)
: data_(tc.data_)
{
std::cout << "TestClass(const TestClass&)\n";
}

TestClass& operator=(int data)
{
std::cout << "TestClass::operator=(int)\n";
data_ = data;
return *this;
}

private:
int data_;
};

class Test1
{
public:
Test1()
: tc_(10)
{
}

private:
TestClass tc_;
};

class Test2
{
public:
Test2()
{
tc_ = 10;
}

private:
TestClass tc_;
};

int main()
{
Test1 t1;
std::cout << "---------------\n";
Test2 t2;
}

Jan 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
Hi, I have been struggling with this issue for a couple of days and would like to know if some can give me a pointer. I want to initialize a struct with default values and depending on the...
2
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
8
by: John | last post by:
Hello, is there any compiler option for g++ for initializing static members of the class. Due to some unknown reason, static member in one of our c++ application is not getting initialized...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.