10

I've been searching a lot for an answer and can´t find it anywere. Say i have:

class foobar{
  public:
     char foo() const;
};

, in my foobar.h

When I want to implement this class in foobar.cpp should I repeat const?:

char foobar::foo() const{
//...my code
}

Or can i do (whitout the const)

char foobar::foo() {
//...my code
}

If this is a duplicate I'm sorry, but no other question truly answered this.

0

6 Answers 6

11

Yes, you have to include the const qualifier in the definition. If you write:

class Foo
{
public:
    int f () const;
};

And in implementation file if you write:

int Foo::f () { /*...*/ }

then the compiler will emit an error saying that there is not function with signature int f () in the class. If you put the const keyword in your implementation file too, it will work.

It is possible to overload functions according to the object constness. Example:

class Foo
{
public:
    int foo ()       { std::cout << "non-const foo!" << std::endl; }
    int foo () const { std::cout << "const foo!" << std::endl; }
};

int main ()
{
    Foo f;
    const Foo cf;
    f.foo ();
    cf.foo ();
}

The output will be (as expected):

non-const foo!

const foo!

As we did with const, you can overload a function based on the object volatileness too.

Sign up to request clarification or add additional context in comments.

2 Comments

why would the output be different for the same operation?
Sorry, I wrote 'Foo cf' instead of 'const Foo cf' in the code. So with const objects, the const function will be called, with non-const objects, the non-const function will be called.
11

You absolutely must include the const qualifier in the implementation.

It is possible to overload functions according to their constness. In fact this is an important part of the language.

2 Comments

can you give me a reference link?
It's not definitive but cs.unm.edu/~storm/C++/ProgrammingTerms/FunctionSignatures.html might help. Or Google "function signatures".
4

Yes, because const is part of the signature. You can define two member functions that differ only in const-ness.

The easiest way to see that it is required is to test your code with your favorite compiler - the code below will not compile:

struct foobar{
    char foo() const;
};
char foobar::foo() {
    return 'a';
}
int main() {
    foobar().foo();
    return 0;
}

You should get an error similar to this:

error: prototype for 'char foobar::foo()' does not match any in class 'foobar'

char foobar::foo()

Demo.

Comments

3

Yes, you should repeat const, it is part of the function signature. It is also possible to have another declaration and implementation without const modifier.

1 Comment

can you give me a reference link?
2

If your variable is char foo() const; and you want to implement it you have to name it as same as you declared it.

char foobar::foo() const{
//...my code
}

char foobar::foo(); is different to char foobar::foo() const;. It means you can have them both in your class declaration like :

class foobar{
  public:
     char foo() const;
     char foo();
};

Comments

2

Yes, must need const qualifier in foobar.cpp file. when you use const in the method signature, you are telling the compiler that memory pointed to by this can't be changed by this method (which is foobar here).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.