Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to refer to my base class object from derived (without cast?)

20 views
Skip to first unread message

Charlie R

unread,
Mar 29, 2023, 6:05:24 AM3/29/23
to
From the derived class, I want to access my base class - the actual
*object* of the base-class part of this.

One thing that DOES work is to static_cast the (this) pointer into the
base class type, but that seems very sketchy (is this even always
correct?).

Example - how to fix the 2 "fail" lines?

#include <iostream>

class base {
public:
operator int() const { return 123; }
};

class child : public base {
public:
void show() {
std::cout << this->base::operator int() <<
"\n"; // works
std::cout << this->base << "\n"; // fails - I
want to refer to my parent object
base & parentobj = this->base; // fails too
base & parentobj2 = *
static_cast<base*>(this); // works... ugly?
}
};

int main() {
child obj;
obj.show();
}

Ralf Fassel

unread,
Mar 29, 2023, 6:13:31 AM3/29/23
to
* Charlie R <charl...@wp.pl>
| From the derived class, I want to access my base class - the actual
| *object* of the base-class part of this.
>
| One thing that DOES work is to static_cast the (this) pointer into the
| base class type, but that seems very sketchy (is this even always
| correct?).
>
| Example - how to fix the 2 "fail" lines?
--<snip-snip>--
| std::cout << this->base << "\n"; // fails - I want to refer to my parent object

std::cout << this->operator int() << "\n";
std::cout << operator int() << "\n";

| base & parentobj = this->base; // fails too

base & parentobj = *this;
std::cout << parentobj.operator int() << "\n";

HTH
R'

Charlie R

unread,
Mar 29, 2023, 6:21:55 AM3/29/23
to
Wed, 29 Mar 2023 12:13:14 +0200, Ralf Fassel:

> * Charlie R <charl...@wp.pl>
> | From the derived class, I want to access my base class - the actual |
> *object* of the base-class part of this.
>>
> | One thing that DOES work is to static_cast the (this) pointer into the
> | base class type, but that seems very sketchy (is this even always |
> correct?).
>>
> | Example - how to fix the 2 "fail" lines?
> --<snip-snip>--
> | std::cout << this->base << "\n"; // fails - I want to refer to my
> parent object
>
> std::cout << this->operator int() << "\n";
> std::cout << operator int()

thanks.
Though, what when we have 2 base classes and both have int() conversion
operator?

Is there no direct way of saying "take my base object X" in an expression?
Is using the static_cast<type_of_base_class*>(this) the valid way?

Sam

unread,
Mar 29, 2023, 6:51:31 AM3/29/23
to
Charlie R writes:

> > std::cout << this->operator int() << "\n";
> > std::cout << operator int()
>
> thanks.
> Though, what when we have 2 base classes and both have int() conversion
> operator?

base1 &obj = *this;

or

base2 &obj = *this;

Then obj.operator int().

> Is there no direct way of saying "take my base object X" in an expression?
> Is using the static_cast<type_of_base_class*>(this) the valid way?

A cast is the only way to convert X to Y, in an expression.

Alf P. Steinbach

unread,
Mar 29, 2023, 7:46:27 AM3/29/23
to
On 2023-03-29 12:21 PM, Charlie R wrote:
> Wed, 29 Mar 2023 12:13:14 +0200, Ralf Fassel:
>
>> * Charlie R <charl...@wp.pl>
>> | From the derived class, I want to access my base class - the actual |
>> *object* of the base-class part of this.
>>>
>> | One thing that DOES work is to static_cast the (this) pointer into the
>> | base class type, but that seems very sketchy (is this even always |
>> correct?).
>>>
>> | Example - how to fix the 2 "fail" lines?
>> --<snip-snip>--
>> | std::cout << this->base << "\n"; // fails - I want to refer to my
>> parent object
>>
>> std::cout << this->operator int() << "\n";
>> std::cout << operator int()
>
> thanks.
> Though, what when we have 2 base classes and both have int() conversion
> operator?
>
> Is there no direct way of saying "take my base object X" in an expression?
> Is using the static_cast<type_of_base_class*>(this) the valid way?

You can and should then do

Base::operator int()

... or in external code

obj.Base::operator int()

- Alf

0 new messages