user1581100
user1581100

Reputation:

Trouble with constant objects and functions

I am trying to learn how to use constant functions and objects, however, I have some error that has kept me up for over an hour and I can't seem to figure out. I was following a simple example and I guess I got lost somewhere along the way. Here is my code.

Main.cpp

#include <iostream>
#include "ExampleClass.h"

int main(){
    ExampleClass exampleObj; // object used to call members of ExampleClass.
    exampleObj.printText(); // calls printVar from the ExampleClass.

    const ExampleClass constantObject; // object used to call constant members of ExampleClass.
    constantObject.printConstText(); // calls printConstVar from the ExampleClass.

    return 0;
}

ExampleClass.h

#ifndef EXAMPLECLASS_H
#define EXAMPLECLASS_H


class ExampleClass
{
    public:
        void printText();
        void printConstText() const;
};

#endif // EXAMPLECLASS_H

ExampleClass.cpp

#include <iostream>
#include "ExampleClass.h"

void ExampleClass::printText(){
    std::cout << "The code works!" << "\n";
}

void ExampleClass::printConstText() const{
    std::cout << "The code works!" << "\n";
}

And I'm getting the error:

C:\Documents and Settings\Me\My Documents\ConstObjects\main.cpp||In function 'int main()':|
C:\Documents and Settings\Me\My Documents\ConstObjects\main.cpp|8|error: uninitialized const 'constantObject'|
||=== Build finished: 1 errors, 0 warnings ===|

If I take out the const before ExampleClass the code executes fine. But is it still a constant object? Thanks for the help, I hope I gave enough information. If it matters at all I'm using Code Blocks.

Upvotes: 8

Views: 230

Answers (3)

Maksim Skurydzin
Maksim Skurydzin

Reputation: 10541

Your ExampleClass is a POD (plain old data) structure. When it's declared as a local variable like this ExampleClass exampleObj no default constructor gets called and it remains uninitialized.

You need either to create a default constructor of your own or use the following syntax -ExampleClass exampleObj = ExampleClass();. This will create a temporary ExampleClass object and value initialize your exampoleObj.

updated:

Here is an excerpt from C++03 standard 8.5.9.

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized;

if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. [this one applies to const objects]

Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value); if the object or any of its subobjects are of const-qualified type, the program is ill-formed. [this one applies to const and POD types]

This means that the constantObject should have user-defined default constructor, otherwise a program is ill-formed, which should be diagnosed. If we remove const, the object will remain uninitialized anyway (will have indeterminate initial value)

Upvotes: 5

juanchopanza
juanchopanza

Reputation: 227418

This behaviour is considered and issue and seems to have been fixed, at least in newer versions of GCC, and presumably in the C++11 standard. See here for the issue report.

Upvotes: 1

Lwin Htoo Ko
Lwin Htoo Ko

Reputation: 2406

The const object "constantObject" needs an initializer or requires "class ExampleClass" to have a user-declared default constructor.

Upvotes: 6

Related Questions