Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A quick check...

Reply
Thread Tools

A quick check...

 
 
Tomás
Guest
Posts: n/a
 
      03-08-2006
class Base
{
public:

int* const p;

Base(int* const x) : p(x) {}
};

class Derived : public Base
{
public:

int monkey[8];

Derived() : Base(monkey) {}
};

int main()
{
Derived k;
}


Is this okay? Can I be certain that "monkey" will have been created before
the constructor of the Base class is called?

-Tomás
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-08-2006
Tomas wrote:

> class Base
> {
> public:
>
> int* const p;
>
> Base(int* const x) : p(x) {}
> };
>
> class Derived : public Base
> {
> public:
>
> int monkey[8];
>
> Derived() : Base(monkey) {}
> };


> Is this okay? Can I be certain that "monkey" will have been created before
> the constructor of the Base class is called?


Construction happens from top to bottom. Base will fully construct, with the
address where monkey will be, before monkey constructs.

However, monkey already has storage, so it can point to it.

New question; how defined is this?

Base(int* x) : p(x)
{
*p;
p[3] = 42;
int whatever = p[4];
}

All those lines deference valid storage containing an unconstructed int.
Ints have trivial constructors, but I thought I heard that all three of
those lines generate technically undefined behavior.

They will probably "work". Don't do any of this in professional code.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-08-2006
Tomás wrote:
> class Base
> {
> public:
>
> int* const p;
>
> Base(int* const x) : p(x) {}
> };
>
> class Derived : public Base
> {
> public:
>
> int monkey[8];
>
> Derived() : Base(monkey) {}
> };
>
> int main()
> {
> Derived k;
> }
>
>
> Is this okay? Can I be certain that "monkey" will have been created before
> the constructor of the Base class is called?


It is OK. The storage for 'Derived' is allocated before the constructor
is invoked. So, 'monkey' array is allocated and the address of it is well
known. Passing that address to the base class for storing is OK.

This practice, however, is shady because if I attempt to access 'x' for
whatever reason in 'Base's constructor, then the behaviour is undefined.

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding quick-release to a non-quick-release tripod head ste7esmith@hotmail.com Digital Photography 4 11-20-2006 03:19 PM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
Quick Restore for a Compaq not so quick! Croos Bustamunky Computer Support 2 05-15-2004 04:17 AM
PanasonicBQ390 "quick" charger - How quick? Ol' Bab Digital Photography 1 01-17-2004 06:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57