const and virtual usage

J

john smith

Hi, say I have a class like so:

class A {
public:
virtual void foo();
void bar() const;
};

so when I actually define foo() and bar(), do I have to re-type the virtual
and const modifiers respectively? Is it more a matter of style?

So is this necessary?:
virtual void foo() {
//do something
}
void bar() const {
// do something
}

thanks in advance.

Kevin
 
M

MiniDisc_2k2

john smith said:
Hi, say I have a class like so:

class A {
public:
virtual void foo();
void bar() const;
};

so when I actually define foo() and bar(), do I have to re-type the virtual
and const modifiers respectively? Is it more a matter of style?

NO! Exactly the opposite!
So is this necessary?:
virtual void foo() {
//do something
}

It is not necessary. In fact, it is illegal. You cannot put the virtual type
modifier here, only in the class definition. virtual only goes there, not in
the definition.

void bar() const {
// do something
}

This is how you must do it. const MUST be in the definition, unlike virtual.
 

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
474,127
Messages
2,570,757
Members
47,315
Latest member
robertsoker

Latest Threads

Top