By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Code still compile when there is a mis-match in .h and .cpp

P: n/a
Hi,

In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);
};

But In my .cpp, I do this:

void A::f1(B* bPtr) {
//....
}

My question is I miss 'const' in my implementation (.cpp), why the
code still compile?

Feb 21 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* yi*****@gmail.com:
Hi,

In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);
};

But In my .cpp, I do this:

void A::f1(B* bPtr) {
//....
}

My question is I miss 'const' in my implementation (.cpp), why the
code still compile?
If you had the 'const' in a slightly different place, the compiler would
have been right.

As it is, perhaps there are options you can turn on to make it more
standard-conforming.

Or, use some other compiler.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 21 '07 #2

P: n/a
On 21 Feb, 17:19, "ying...@gmail.com" <ying...@gmail.comwrote:
Hi,

In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);

};

But In my .cpp, I do this:

void A::f1(B* bPtr) {
//....

}

My question is I miss 'const' in my implementation (.cpp), why the
code still compile?
It shouldn't compile, for exactly the reason you suggest - unless, of
course, your class A *also* contains an overload of f1 declared as
void f1 (B* bPtr);
in the class.

If not, maybe something else in your code is causing your confusion.
You've posted a snippet that I can't directly compile so I don't know
what else is in your code. See posting guidelines at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Try this complete program. Does it compile for you? What compiler are
you using?

class B {};

class A{
public:
void f1 (const B* bPtr);
};
void A::f1(B* bPtr) {}

int main() {}

Gavin Deane

Feb 21 '07 #3

P: n/a
On Feb 21, 11:25 am, "Alf P. Steinbach" <a...@start.nowrote:
* ying...@gmail.com:
Hi,
In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);
};
But In my .cpp, I do this:
void A::f1(B* bPtr) {
//....
}
My question is I miss 'const' in my implementation (.cpp), why the
code still compile?

If you had the 'const' in a slightly different place, the compiler would
have been right.

As it is, perhaps there are options you can turn on to make it more
standard-conforming.

Or, use some other compiler.
Thanks. Can you please tell me what is the place where the compiler
would have been right?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Feb 21 '07 #4

P: n/a
* yi*****@gmail.com:
On Feb 21, 11:25 am, "Alf P. Steinbach" <a...@start.nowrote:
>* ying...@gmail.com:
>>Hi,
In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);
};
But In my .cpp, I do this:
void A::f1(B* bPtr) {
//....
}
My question is I miss 'const' in my implementation (.cpp), why the
code still compile?
If you had the 'const' in a slightly different place, the compiler would
have been right.

As it is, perhaps there are options you can turn on to make it more
standard-conforming.

Or, use some other compiler.
Thanks. Can you please tell me what is the place where the compiler
would have been right?
Why? I have the feeling that the original question was contrived. That
you simply /got the question wrong/.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.