Topic: Passing a reference to a constructor
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/11/27 Raw View
In article <81hcd0$79d$1@nnrp1.deja.com>, nishd@my-deja.com writes
>I have the following code
>
>int main(void)
>{
> int a = 1;
> myClass(a);
>}
>
>class myClass
>{
>public:
>myClass(int & temp) {};
>}
>
>This code does not compile using VC++ 6.0 unless I cast "a" to
>type "int &"
>like so:
> myClass((int &)a);
>
>Is this normal behavior - does the standard specify having to cast to
>the reference type when passing as reference?
The compiler is wrong, but is entitled to suspect your code because it
can see that you are passing an argument to a named parameter yet you
make no use of it. Does the compiler still issue a diagnostic if:
1) you leave the parameter unnamed
or
2) you actually make use of it
?
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/27 Raw View
nishd@my-deja.com wrote:
>
> I have the following code
>
> int main(void)
> {
> int a = 1;
> myClass(a);
> }
>
> class myClass
> {
> public:
> myClass(int & temp) {};
> }
>
> This code does not compile using VC++ 6.0 unless I cast "a" to
> type "int &"
> like so:
> myClass((int &)a);
The code as you show it tries to use myClass before it is defined.
In that case, the code should not compile at all. You are also missing
a semicolon on the class definition.
First, let's rearrange the code so it could possibly be valid:
class myClass
{
public:
myClass(int & temp) {};
}; // semicolon added
int main(void)
{
int a = 1;
myClass(a);
}
The last line of main could be interpreted in two ways:
1. Creation of an anonymous object of type MyClass, initialized
with local variable 'a'.
2. Declaration of an object 'a' of type myClass. The parentheses are
redundant but allowed.
The rule in C++ is that if a statement syntactically could be a
declaration or an expression, it is a declaration. The code therefore
tries to create a second object named 'a' in the same scope as integer
'a', and is thus invalid. It is also invalid because there is no
constructor for myClass that can be invoked without arguments. The
fact that the declaration is semantically invalid does not prevent
it from being viewed as a declaration.
When you change the line to
myClass( (int&) a );
it can no longer be a declaration of an object 'a', because the
outer type in a declaration can't be enclosed in parentheses. Now
the statement can only be the creation of an anonymous object of type
myClass, initialized with local object 'a'.
Possibly you are creating this anonymous object for the purposes of
the side effects of its creation and destruction. Or maybe you forgot
to provide the name of the local variable:
myClass m(a); // declare local object m of type myClass
Or perhaps you were trying to call the myClass constructor as a
normal function. You can't do that; constructors can't be called
like other functions. (Technically, constructors don't have names;
when you write the "name" of a constructor, it is found only as the
name of a type, not the name of a constructor.) You can invoke a
constructor only in conjunction with creating an object of the
class type.
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: nishd@my-deja.com
Date: 1999/11/27 Raw View
I have the following code
int main(void)
{
int a = 1;
myClass(a);
}
class myClass
{
public:
myClass(int & temp) {};
}
This code does not compile using VC++ 6.0 unless I cast "a" to
type "int &"
like so:
myClass((int &)a);
Is this normal behavior - does the standard specify having to cast to
the reference type when passing as reference?
thanks
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]