simple question regarding constructors

Pages: 12
what are some things you cannot do inside a constructor?
I do not know such things except that you may not take the address of the constructor.
can i use loops, mathematical statements , etc?
Yup.
Constructor in fact is an usual function except that

10 No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value. The address of a constructor shall not be taken
(C++ Standard)
closed account (Dy7SLyTq)
you cant feed your snail in a constructor
(before any one freaks out, the question has been answered and i am referencing sponge bob)
the constructor must also have the same name as the object in question.

struct dude, must have constructor dude::dude, not dude::whatsup
Call virtual methods. That's the answer I think you're after.
Call virtual methods.


You can call virtual methods in constructors. The result may be surprising, but you can definitely call them.

http://www.parashift.com/c%2B%2B-faq-lite/calling-virtuals-from-ctors.html
I'm well aware that it compiles...

Effective C++ recommends not to do this:
http://www.artima.com/cppsource/nevercall.html

Google's style guide recommends not to do this:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Doing_Work_in_Constructors

C++lint error 1506 warns about it:
http://stellar.cleanscape.net/products/cpp/checks.html
how about "call pure virtual methods"? That can't be done from ctors. Or dtors for that matter.
Perhaps someone could outline what should be done in constructors, rather than mention what can potentially done. AFAIK the main idea is that constructors are for initialising member variables, and really not for anything else - so no implementation code. I might start by mentioning these things:

1. Simple initialisation via the initialiser list, including calling base class constructors;
2. Complex initialisation via a call to an Initialise() function;
3. Complex initialisation of variables common to all the constructors via a call to an InitCommon() function;
4. Complex initialisation of variables specific to all this constructor via a call to a different Init() function.

With number 2, Google have this because they don't wish to use exceptions. If that is not the case then I don't see why this couldn't be in the constructor. But, one then has to make sure to handle any exceptions that may arise. One example of complex initialisation: In my current project, I have a bunch of static constexpr's

Any thing else should be in a different member function.

Hopefully I am much less discombobulated than yesterday, and am saying the right things, if not feel free to shoot down what I have mentioned 8+)
@Disch

how about "call pure virtual methods"? That can't be done from ctors. Or dtors for that matter.


You are wrong.

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
#include <iostream>

struct A
{
	virtual ~A() {}
	virtual void f() const = 0;
};

void A::f() const
{
	std::cout << "I'm a pure virtual function.\n";
}

struct B : A
{
	B()
	{
		std::cout << "Let look what will be if we call the pure virual function:)\n";
		A::f();
	}
	void f() const
	{
	}
};

int main()
{
	B();
}
closed account (Dy7SLyTq)
struct B : A


did you mean to write public? if not what access does b have in terms of a?
@DTSCode

struct B : A


did you mean to write public? if not what access does b have in terms of a?


What I wrote is what I meant.
closed account (Dy7SLyTq)
then could you please answer what access does b have in terms of a?
What does "in terms of a" mean? What is "a'? I do not understand you.
struct B : A {}

is equivalent to

struct B : public A {}
closed account (Dy7SLyTq)
lets say for a moment you had written
struct b : public a. b would have access to a's public members. ie:

b myb;
myb.some_public_method_from_a();
//i know its not real its just an example.

what does b get from a when you write
struct b : a?

edit i didnt see the above one because i was writing it at the same time as you.
......
thanks for clearing that up for me
Last edited on
vlad wrote:
You are wrong.


Not really. In your example, 'f' is not pure virtual in B.
Pages: 12