473,761 Members | 10,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using "this" in a base initializer list?

Hello! Is the following code illformed or does it yield undefined
behaviour:

class a

{};

class b

{

public:

b(a&){}

};

class c: private a, private b

{

public:

c():b(*this){}

};

May 8 '07 #1
10 2741
Angel Tsankov wrote:
Hello! Is the following code illformed or does it yield undefined
behaviour:

class a

{};

class b

{

public:

b(a&){}

};

class c: private a, private b

{

public:

c():b(*this){}

};
Neither. The code is fine.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 8 '07 #2
On May 8, 9:21 am, "Angel Tsankov" <fn42...@fmi.un i-sofia.bgwrote:
Hello! Is the following code illformed or does it yield undefined
behaviour:

class a

{};

class b

{

public:

b(a&){}

};

class c: private a, private b

{

public:

c():b(*this){}
Ambiguous call. b(const b&) and b(a&)
>

};- Hide quoted text -

- Show quoted text -

May 8 '07 #3
siddhu wrote:
On May 8, 9:21 am, "Angel Tsankov" <fn42...@fmi.un i-sofia.bgwrote:
>Hello! Is the following code illformed or does it yield undefined
behaviour:

class a

{};

class b

{

public:

b(a&){}

};

class c: private a, private b

{

public:

c():b(*this) {}

Ambiguous call. b(const b&) and b(a&)
You're right! How did I miss that? <redface>
>

>>

};- Hide quoted text -

- Show quoted text -
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 8 '07 #4
On May 8, 3:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Angel Tsankov wrote:
Hello! Is the following code illformed or does it yield undefined
behaviour:
class a
{};
class b
{
public:
b(a&){}
};
class c: private a, private b
{
public:
c():b(*this){}
};
Neither. The code is fine.
Are you sure? Throw in some virtual inheritance, and it core
dumps with Sun CC. And §3.8 of the standard explicitly says
that:

Before the lifetime of an object has started but after
the storage which the object will occupy has been
allocated39) or, after the lifetime of an object has
ended and before the storage which the object occupied
is reused or released, any pointer that refers to the
storage location where the object will be or was located
may be used but only in limited ways.[...] If the
object will be or was of a non-POD class type, the
program has undefined behavior if
[...]
-- the pointer is implicitly converted (4.10) to a
pointer to a base class type,

(Quoted from the April, 2006 draft. The only one I have
handy here. But I don't think the text in question has
changed since the original standard.)

I think that there are some special rules concerning the
this pointer, but I can't find them off hand. If you can
find them, and point them out to me, I'd be very happy.
I really want this to be defined.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 8 '07 #5


--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"siddhu" <si***********@ gmail.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
On May 8, 9:21 am, "Angel Tsankov" <fn42...@fmi.un i-sofia.bg>
wrote:
>Hello! Is the following code illformed or does it yield
undefined
behaviour:

class a

{};

class b

{

public:

b(a&){}

};

class c: private a, private b

{

public:

c():b(*this) {}

Ambiguous call. b(const b&) and b(a&)
>>

};- Hide quoted text -
Well, I meant this:

class c: private a, private b
{
public:
c():b(static_ca st<a&>(*this)){ }
};

May 8 '07 #6
James Kanze wrote:
On May 8, 3:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>Angel Tsankov wrote:
>>Hello! Is the following code illformed or does it yield undefined
behaviour:
>>class a
{};
>>class b
{
public:
>>b(a&){}
};
>>class c: private a, private b
{
public:
c():b(*this){ }
};
>Neither. The code is fine.

Are you sure? Throw in some virtual inheritance, and it core
dumps with Sun CC. [..]
Huh? Throw in division by zero or dereferencing a null pointer
and it may release nasal demons. What's it got to do with the
code at hand?
May 8 '07 #7
* Victor Bazarov:
James Kanze wrote:
>On May 8, 3:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>>Angel Tsankov wrote:
Hello! Is the following code illformed or does it yield undefined
behaviour:
class a
{};
class b
{
public:
b(a&){}
};
class c: private a, private b
{
public:
c():b(*this) {}
};
Neither. The code is fine.
Are you sure? Throw in some virtual inheritance, and it core
dumps with Sun CC. [..]

Huh? Throw in division by zero or dereferencing a null pointer
and it may release nasal demons. What's it got to do with the
code at hand?
James is referring to the conversion from c* to a*, which §3.8/5 says is
Undefined Behavior. But the interpretation that yields that conclusion
also means that this,

struct S
{
void foo() {}
S() { this->foo(); }
};

is undefined behavior, so that the common technique of using an
init-function called from multiple constructors, would be formally
Undefined Behavior. Which really means that part of the standard is
fishy Fishy FISHY, and should not be taken verbatim: some constructive
interpretation required, and I think it's impossible to say whether the
code above is formally OK (or not), only that it's in-practice OK.

--
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?
May 8 '07 #8
Alf P. Steinbach wrote:
* Victor Bazarov:
>James Kanze wrote:
>>On May 8, 3:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Angel Tsankov wrote:
Hello! Is the following code illformed or does it yield undefined
behaviour :
class a
{};
class b
{
public:
b(a&){}
};
class c: private a, private b
{
public:
c():b(*this ){}
};
Neither. The code is fine.
Are you sure? Throw in some virtual inheritance, and it core
dumps with Sun CC. [..]

Huh? Throw in division by zero or dereferencing a null pointer
and it may release nasal demons. What's it got to do with the
code at hand?

James is referring to the conversion from c* to a*, which §3.8/5 says
is Undefined Behavior.
Yes, but where did you find that conversion? There is reference
binding (8.5.3/5), but no pointer conversion.
But the interpretation that yields that
conclusion also means that this,

struct S
{
void foo() {}
S() { this->foo(); }
};

is undefined behavior, so that the common technique of using an
init-function called from multiple constructors, would be formally
Undefined Behavior. Which really means that part of the standard is
fishy Fishy FISHY, and should not be taken verbatim: some constructive
interpretation required, and I think it's impossible to say whether
the code above is formally OK (or not), only that it's in-practice OK.
<shrug The reference (or pointer) is not being converted/used in the
code at all, why all the fuss?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 8 '07 #9
On May 8, 8:16 pm, "Alf P. Steinbach" <a...@start.now rote:
* Victor Bazarov:
James Kanze wrote:
On May 8, 3:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Angel Tsankov wrote:
Hello! Is the following code illformed or does it yield undefined
behaviour:
class a
{};
class b
{
public:
b(a&){}
};
class c: private a, private b
{
public:
c():b(*this){ }
};
Neither. The code is fine.
Are you sure? Throw in some virtual inheritance, and it core
dumps with Sun CC. [..]
Huh? Throw in division by zero or dereferencing a null pointer
and it may release nasal demons. What's it got to do with the
code at hand?
James is referring to the conversion from c* to a*, which §3.8/5 says is
Undefined Behavior. But the interpretation that yields that conclusion
also means that this,
struct S
{
void foo() {}
S() { this->foo(); }
};
is undefined behavior, so that the common technique of using an
init-function called from multiple constructors, would be formally
Undefined Behavior. Which really means that part of the standard is
fishy Fishy FISHY,
You get that feeling too. Realistically, at least some of the
uses must be defined, regardless of what the standard says. But
which ones?
and should not be taken verbatim: some constructive
interpretation required, and I think it's impossible to say whether the
code above is formally OK (or not), only that it's in-practice OK.
In this simple case. That's why I mentionned that I'd actually
had a problem with a real compiler in the past. It's because I
had the problem that I'd familiarized myself with the standard,
too. I wanted to complain about a compiler error, but the
standard didn't support me.

Logically, I still have trouble understanding *why* it should be
undefined behavior. The compiler must be able to do the
conversion, since it has to get the correct address for the this
pointer it passes to the base class constructors. In my case,
the code was very clear:

class MyStream
: public virtual MyStreambuf
, public std::ostream
{
} ;

The organization was such in order to ensure that the streambuf
was constructed before its address was passed to std::ostream.
Initializing std::ostream with this, however, resulted in the
wrong address being passed, and a core dump later, when ostream
used it. Removing the virtual worked. What also worked was
wrapping the streambuf:

template< typename Streambuf >
class StreambufWrappe r
{
public:
std::streambuf* getStreambuf() { return &myStreambuf ; }
Streambuf myStreambuf ;
} ;

class MyStream
: public virtual StreambufWrappe r< MyStreambuf >
: public std::ostream
{
public:
MyStream() : std::ostream( getStreambuf() ) {}
} ;

Obviously, the compiler had to do successfully convert this to a
pointer to the base class in order to call getStreambuf(), but
for who knows what reason, it was unable to do it correctly when
this was passed to the constructor.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2261
by: Ernst Murnleitner | last post by:
Dear Readers, Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted? Reason: I changed my program from using normal pointers to classes A, ... typedef A * APtr;
0
4353
by: Colleyville Alan | last post by:
My app is giving me this error. Run-time error 3211: The database engine could not lock table 'Sorted_Template' because it is already in use by another person or process. When I run the app the first time through, it runs fine. I get this when I run a make-table query that tries to write out the table called "Sorted_Template". I tried using a TableDefs.Delete command thinking that make using the docmd.openquery was causing the...
9
2370
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that and get
6
5493
by: Thomas H | last post by:
Hi everyone, I've got a question that's more of a "style" question... Do you guys reference "this" for every object that's inherited from System.Web.UI.Page? For example, when you use the Session object, do you say "this.Session.property" or do you just say "Session.property", and leave off "this"? I'm tending towards always using "this.Session" and "this.Response" and "this.Label5.Text" - even though I don't have to. I have the...
7
2249
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
60
5059
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
19
7927
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache limiter - headers already sent in /home/httpd/vhosts/monkeyclaus.org/httpdocs/media/audio/pdsIncludes/CommandStartSession.php on line 14
2
4744
by: Dustin | last post by:
I found a solution to my problem in the topic called "OOP (and the XMLHttpRequest)". You may find it useful to reference to see what I have done so far. I am making a small AJAX helper object called AjaxManager. The purpose is to make ajax requests easier to use and it also queues them, so you can tell it to run three requests, one after another and it won't do the next until the previous is completed. The problem is here, where the...
14
2122
by: Alexander Dong Back Kim | last post by:
Dear all, I used to use C++ programming language at all time but moved to C# and Java. Few days ago, I restarted studying about C++ with a very beginner's mind. I wrote a simple class and gcc couldn't compile the class. Any hints that I'm missing? Header File: #ifndef __Calc_h__
3
2586
dlite922
by: dlite922 | last post by:
Hey guys, My brains asleep and I don't know what's wrong with my session class. I'm over riding session with sesstion_set_save_handler() in a class; When in my member functions (open, close, read) I use "$this" I get the error "Using $this when not in object context". Here's my constructor:
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.