|
|
Can you explicitly call an implicitly defined operator =( ) ?
Can someone tell me if this is legal or not, in respect of B::operator
=( ) calling the implicitly defined A::operator =( )? I am using this
technique in some C++ that Borland C++ chokes on.
thanks
Kevin
---------------
class A
{
private:
int a;
};
class B : public A
{
private:
int b;
public:
B& operator =( const B& _r_Source )
{
if( &_r_Source != this )
{
A::operator =( _r_Source );
b = _r_Source.b;
}
}
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kevin_g_frey (24)
|
3/1/2006 1:21:26 PM |
|
kevin_g_frey@hotmail.com wrote:
> Can someone tell me if this is legal or not, in respect of
> B::operator =( ) calling the implicitly defined A::operator
> =()? I am using this technique in some C++ that Borland C++
> chokes on.
It's fully legal if A is a class type (defined by means of
class, struct or union) -- operator= is a function in every
case, even when it is the compiler which implicitly declares and
defines it. I'm less sure about things like int::operator=(); I
don't think they are legal. (Of course, there's never any
reason to do this in normal code, but perhaps some special case
in templates...)
> ---------------
> class A
> {
> private:
> int a;
> };
> class B : public A
> {
> private:
> int b;
> public:
> B& operator =( const B& _r_Source )
> {
> if( &_r_Source != this )
> {
> A::operator =( _r_Source );
> b = _r_Source.b;
> }
> }
> };
No problem here (except that the test for self assignment isn't
necessary -- and typically, when it is necessary, it's a sign of
a broken operator=).
--
James Kanze GABI Software
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kanze
|
3/1/2006 4:56:54 PM
|
|
kevin_g_f...@hotmail.com wrote
> Can someone tell me if this is legal or not, in respect of B::operator
> =( ) calling the implicitly defined A::operator =( )?
Yes this is perfectly legal. In fact it compiles very cleanly in
VSNet2003.
Also you can observe that it works correctly as expected.
However I noticed that you have not returned the dereferenced this
pointer.
So your copy assignment operator should have a
return *this ;
at the end .
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
RenjithMohan
|
3/1/2006 5:01:02 PM
|
|
|
2 Replies
507 Views
(page loaded in 0.045 seconds)
Similiar Articles: Can you explicitly call an implicitly defined operator ...Can someone tell me if this is legal or not, in respect of B::operator =( ) calling the implicitly defined A::operator =( )? I am using this technique... Why can not I implicitly convert "Derived **" to "Base * const ...Can you explicitly call an implicitly defined operator ... Ofcourse, only types that can convert to ... only) - IBM - United States It is implicitly defined; All the ... Avoiding problems with short-circuit primitive operator&& versus ...Can you explicitly call an implicitly defined operator ... Avoiding problems with short-circuit primitive operator&& versus ... Ofcourse, only types that can convert to ... reordering expressions with NaN - comp.lang.fortranCan you explicitly call an implicitly defined operator ... reordering expressions with NaN - comp.lang.fortran... conformamce to the rules of NaN handling can kill ... C++0X explicitly defaulted copy constructors - comp.lang.c++ ...> Note that you can employ a trick here. Your ... And since A(A&) would not be implicitly declared, I can't default ... From the draft standard: "A pointer can be explicitly ... VHDL-2002 vs VHDL-93 vs VHDL-87? - comp.lang.vhdlPSL is defined by IEEE 1850-2005 ... The operators ?= etc. N.B. the ?? operator can ... of ways that a composite subtype can be unconstrained. The "implicitly ... Oracle SQL bug in 9.2.0.8 - comp.databases.oracle.server ...> Also, you can see that the spaces are still ... is a given type: always try to explicitly CAST it. ... to a > > column in a table which has a defined type), Oracle implicitly ... nitializing a static vector <> of integers (this static vector... list and I would like to see if you can assist ... The above send call can then call the BSD ... the intent of std::vector (e.g. that operator[] be as fast as possible) can ... Why do pointer values display as ASCII characters instead of HEX ...... implicitly conversion to void*, so the second is chosen. In order to output a char* as a pointer and not as a string, you can simply convert it explicitly ... defined ... You Can ... Use of MATLAB fftshift - comp.dspYou can see this if you do the inverse fft of ... operator[](size_t i){return this->operator[](i-1);}; }; You would ... and exponentials (on square matrices) can be defined ... Can you explicitly call an implicitly defined operator ...Can someone tell me if this is legal or not, in respect of B::operator =( ) calling the implicitly defined A::operator =( )? I am using this technique... GotW #69: Enforcing Rules for Derived Classes - GotW.ca Home Page... above, volatile can go hang. An implicitly declared copy assignment operator is only implicitly defined when you actually try to call it ... derived class to explicitly call ... 7/24/2012 12:41:52 PM
|
|
|
|
|
|
|
|
|