N
News
Hi,
Should the following code compile without errors?
class test
{
public:
void display(const int x);
};
void test::display(int num)
{
}
void main()
{
test object;
object.display(10);
}
The function prototype in the class declaration differs from the
implementation as the argument is const in the declaration but not in the
implementation (or the other way round). I know that this is slightly
superfluous as the int is being passed by value anyway & so cannot be
altered in the display() function as a return value. However, there is
still a mismatch! If the argument to display is passed by reference rather
than by value i.e.
class test
{
public:
void display(const int & x);
};
void test::display(int & num)
{
}
void main()
{
test object;
object.display(10);
}
then a compiler error is generated.
I am using the Microsoft C++ compiler (VC++6 & VC++.NET). What do other
compilers do with the first code sample?
Simon
Should the following code compile without errors?
class test
{
public:
void display(const int x);
};
void test::display(int num)
{
}
void main()
{
test object;
object.display(10);
}
The function prototype in the class declaration differs from the
implementation as the argument is const in the declaration but not in the
implementation (or the other way round). I know that this is slightly
superfluous as the int is being passed by value anyway & so cannot be
altered in the display() function as a return value. However, there is
still a mismatch! If the argument to display is passed by reference rather
than by value i.e.
class test
{
public:
void display(const int & x);
};
void test::display(int & num)
{
}
void main()
{
test object;
object.display(10);
}
then a compiler error is generated.
I am using the Microsoft C++ compiler (VC++6 & VC++.NET). What do other
compilers do with the first code sample?
Simon