Using const?

D

desktop

In this code I am trying to make a const instance of class B in main.
But I get the error:

test.c++: In function ‘int main()’:
test.c++:34: error: uninitialized const ‘b’

Why should it be initialized? And how do I initialize an object?


#include <iostream>
class A {
public:
void foo() {
printf("non-const method\n");
}

void foo() const {
printf("const method\n");
}

};

class B {
public:
A a;
void callfoo() {
a.foo();
}

void callfoo() const {
a.foo();
}

};


int main() {
const B b;
return 0;
}
 
D

desktop

desktop said:
In this code I am trying to make a const instance of class B in main.
But I get the error:

test.c++: In function ‘int main()’:
test.c++:34: error: uninitialized const ‘b’

Why should it be initialized? And how do I initialize an object?


#include <iostream>
class A {
public:
void foo() {
printf("non-const method\n");
}

void foo() const {
printf("const method\n");
}

};

class B {
public:
A a;
void callfoo() {
a.foo();
}

void callfoo() const {
a.foo();
}

};


int main() {
const B b;
return 0;
}

Hm if I write a default constructor: 'A(){}' in A or 'B(){}' in B it
works. In my book 'Accelerated C++' there is no mention of the need to
define a default constructor when declaring const object.
 
S

Salt_Peter

In this code I am trying to make a const instance of class B in main.
But I get the error:

test.c++: In function 'int main()':
test.c++:34: error: uninitialized const 'b'

Why should it be initialized? And how do I initialize an object?

#include <iostream>
class A {
public:
void foo() {
printf("non-const method\n");
}

void foo() const {
printf("const method\n");
}

};

class B {
public:
A a;
void callfoo() {
a.foo();
}

void callfoo() const {
a.foo();
}

};

int main() {
const B b;
return 0;

}

A user-declared default constructor is required.

class B
{
A a;
public:
B() { } // def ctor

void callfoo() {
a.foo();
}

void callfoo() const {
a.foo();
}

};

"if the object is of const-qualified type,
the underlying class type shall have a user-declared default
constructor"
 
J

Jerry Coffin

[email protected] says... said:
Hm if I write a default constructor: 'A(){}' in A or 'B(){}' in B it
works. In my book 'Accelerated C++' there is no mention of the need to
define a default constructor when declaring const object.

The class doesn't necessarily have to provide a default ctor. If you
define a const object, you MUST initialize it; that can be done with
either a default ctor or with a non-default ctor, to which you supply
arguments. For example, the following is all fine:

struct A {
A() {}
A(int) {}
};

int main() {
const A a; // uses default ctor
const A a2(2); // supplies argument to non-default ctor
return 0;
}

If the class doesn't have any state, there's no really good reason for
this requirement -- but if the class has no state, creating the const
object doesn't make a lot of sense anyway -- and this allows the
compiler to enforce the requirement when it does matter (which is most
of the time...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top