Constructors

Suppose you were defining a string in the default constructor. Now, suppose you wish to append something to that string in the parametric constructor. Does the parametric constructor automatically erase whatever was defined in the default constructor? It seems to be doing this for me. Now, I would like to know why it would be doing this.
Last edited on
Objects can only be constructed once.

If you are calling the parametric constructor, the default constructor is never called. Therefore the string you assigned in the default constructor never was assigned.


I believe C++11 changes this, allowing you to chain multiple constructors together, but I don't know how widely that is supported.
Only one constructor gets executed, unless your compiler is sufficiently modern to support delegating constructors (a C++11 language feature)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <iostream>

struct Example
{
    std::string content;

    Example() : content("default")
   {
   }

    Example(const std::string& suffix) : Example()
    {   
        content += suffix;
    }
};

int main()
{
    Example e1;
    Example e2(".com");

    std::cout << "Default: " << e1.content << '\n'
              << "Non-default: " << e2.content << '\n';
}

demo: http://liveworkspace.org/code/1oIKoa
Yeah, I was thinking something like, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <new>

class theclass
{
	private:
		std::string mystr;
		//some other stuff
	public:
		theclass();
		theclass(std::string);
		//some more stuff
};	//end class

theclass::theclass()
{
	mystr = "default string value";
}	//end default constructor

theclass::theclass(std::string str)
{
	mystr = str+mystr;
}	//end parametric constructor

//definitions for other stuff before main

int main()
{
	theclass * t = new (nothrow) theclass[2];
	cout << "Before calling the parametric constructor: " << endl;
	for (int x = 0; x < 2; x++) cout << "t[" << x << "].mystr == " << t[x].mystr << endl;
	cout << "\nAfter calling the parameterized constructor: "<<endl;
	t[0].theclass("non-");
	cout << "t[0].mystr == " << t[0].mystr << endl;
	t[1].theclass("post-");
	cout << "t[1].mystr == " << t[1].mystr << endl;
	return 0;
}	//end main 
Also, I was wondering if, once a parametric constructor is called (this constructor is only setting one private variable, mind you), if the better way to set that variable again would be:
a.) From a friend class, directly setting it or
b.) Calling the parametric constructor again with the new value
?
I am resetting the private value to call some of the class's functions again.
Last edited on
You can't call the parametric construct (or any constructor) again. One way is to use a setter.
1
2
3
4
5
6
7
8
9
class theclass
{
private:
  int m_int;
public:
  theclass(int defaultVal) : m_int(defaultVal) {}

  void setDefaultVal(int defaultVal) { m_int = defaultVal; }
}


Something similar in the C++ standard library is opening files with fstream. You can open the file from the constructor:
std::ofstream fout("Myfile.txt");
or you can open the file later with the .open() function
fout.open("Myfile.txt");

I think the constructor probably calls open().
Even if you did something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream> 

using namespace std;

class myclass
{
	private:
		string str;
		//other private declarations
	public:
		myclass();
		myclass (string);
		//other public declarations
};	//end myclass

myclass::myclass()
{
	//initialize everything
}	//end default constructor

myclass::myclass(string thestring)
{
	str = thestring;
}	//end parametric constructor

int main()
{
	//some stuff
	myclass m;
	string parameters[] = {"param1", "param2", "param3"};
	for (int x = 0; x < 3; x++)
	{
		m = myclass(parameters[x]);
		//some other stuff
	}	//end for
	return 0;
}	//end main 

?

EDIT: I do this in my program, and it doesn't throw errors on this, but it consumes memory so fast that it threatens to crash the whole system. I guess I answered my question....
Last edited on
Topic archived. No new replies allowed.