Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about objects passed into constructors

Reply
Thread Tools

question about objects passed into constructors

 
 
fighter1@airmail.net
Guest
Posts: n/a
 
      01-16-2004
Hello,

I have a question about passing an object into a constructor.

How can I check to make sure that the object is good/valid that is
being passed into the constructor.

Obviously I could check to see if it is non-NULL, but that still will
not necessarily tell me anything.

For example:

/* here is a constructor car that takes an engine class as an
argument. But, I better check to make sure that engine is valid
so how do I do that?
*/
Car::Car( Engine* engine )
{

// check to make sure that engine is valid.


}


Thanks in advance for any help,
John
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-16-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I have a question about passing an object into a constructor.
>
> How can I check to make sure that
> the object that is being passed into the constructor is good/valid.
>
> Obviously I could check to see if it is non-NULL
> but that still will not necessarily tell me anything.



class Engine {
private:
// representation
const
unsigned int Valid;
public:
//functions
bool valid(void) const {
return 0x55555555 == Valid;
}
// constructors
Engine(void): Valid(0x55555555) { }
~Engine(void) { Valid = 0xAAAAAAAA; }
};

> For example:
>
> // Here is a constructor car
> // that takes an engine class as an argument.
> // I had better check to make sure that engine is valid.
> // How do I do that?
>
> Car::Car(const Engine& engine) {
> // check to make sure that engine is valid.
> if (engine.valid()) {
> // Initialize *this.
> }
> else { /* Handle error. */ }
> }


 
Reply With Quote
 
 
 
 
Donovan Rebbechi
Guest
Posts: n/a
 
      01-16-2004
In article <(E-Mail Removed)>, (E-Mail Removed) wrote:

> How can I check to make sure that the object is good/valid that is
> being passed into the constructor.
>
> Obviously I could check to see if it is non-NULL, but that still will
> not necessarily tell me anything.
>
> For example:
>
> /* here is a constructor car that takes an engine class as an
> argument. But, I better check to make sure that engine is valid
> so how do I do that?
> */


What do you mean by "valid" ? The Engine class should be responsible for making
sure that Engine objects are not "invalid".

Since you are passing in a pointer, you need to check for a null pointer.

If the pointer is not null, it's the callers responsibility to pass in a valid
pointer. There's no way to check whether a pointer is "wild" or not.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      01-16-2004
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I have a question about passing an object into a constructor.
>
> How can I check to make sure that the object is good/valid that is
> being passed into the constructor.
>
> Obviously I could check to see if it is non-NULL,


Unless it's a pointer there's nothing to check, NULL-wise.

> but that still will
> not necessarily tell me anything.
>
> For example:
>
> /* here is a constructor car that takes an engine class as an
> argument. But, I better check to make sure that engine is valid
> so how do I do that?
> */
> Car::Car( Engine* engine )
> {
>
> // check to make sure that engine is valid.


You'll have to ask the Engine object, if it has a member that serves that
purposes. Other than checking whether the supposed memory the object
occupies is real memory, which is a system-specific matter and OT here,
there is no general way to tell if an object is valid. If the Engine itself
can't tell you it's valid, nothing else can.

DW



 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      01-16-2004
"E. Robert Tisdale" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) wrote:
>
> > I have a question about passing an object into a constructor.
> >
> > How can I check to make sure that
> > the object that is being passed into the constructor is good/valid.
> >
> > Obviously I could check to see if it is non-NULL
> > but that still will not necessarily tell me anything.

>
>
> class Engine {
> private:
> // representation
> const
> unsigned int Valid;
> public:
> //functions
> bool valid(void) const {
> return 0x55555555 == Valid;
> }
> // constructors
> Engine(void): Valid(0x55555555) { }
> ~Engine(void) { Valid = 0xAAAAAAAA; }
> };


Of course, with the same "valid" value used in all instances, and perhaps in
other classes as well, it will greatly increase the probability that some
dangling or uninitialized pointer will be unlucky enough to point somewhere
such that the Valid member happens to be 0x55555555.

DW



 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      01-16-2004

<(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> Hello,
>
> I have a question about passing an object into a constructor.
>
> How can I check to make sure that the object is good/valid that is
> being passed into the constructor.
>
> Obviously I could check to see if it is non-NULL, but that still will
> not necessarily tell me anything.
>
> For example:
>
> /* here is a constructor car that takes an engine class as an
> argument. But, I better check to make sure that engine is valid
> so how do I do that?
> */
> Car::Car( Engine* engine )
> {
>
> // check to make sure that engine is valid.
>
>
> }
>
>
> Thanks in advance for any help,
> John



Most probably, you need to not use a pointer:

Car::Car(Engine const& engine)

as this will assure a proper engine-object to be passed always.

If "Car" does not need an engine, you could keep your present design.

If Car needs to use an engine, but you for some reason want to keep the
signature of the constructor (the reason for this would not be based in the
language), You should throw from the constructor:

Car::Car( Engine* engine)
{
if (!engine) throw 0; // have a proper exception here!!
// continue construction here
}

but be prepared to refactor later.

I can't really recommend Tisdales trick unless there is a compelling reason
to do it that way (e.g. if you are not allowed to throw an exception in the
constructor).

/Peter


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      01-16-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I have a question about passing an object into a constructor.
>
> How can I check to make sure that the object is good/valid that is
> being passed into the constructor.
>
> Obviously I could check to see if it is non-NULL, but that still will
> not necessarily tell me anything.
>

[SNIP]

Well, the whole problem arises from the word "valid". It depends very much
what you define as a valid object. Testing for NULL is a way of checking
whether a pointer is valid but this doesn't tell you whether the object,
which this pointer points to, has a "valid = reasonable" state. If you just
want to check whether the pointer has something to point to then this NULL
test is fine. Otherwise you will have to define what characterizes a
"good/valid" object state and supply some function to check this. (See
Robert Tisdale's post).

Cheers
Chris


 
Reply With Quote
 
Dag Henriksson
Guest
Posts: n/a
 
      01-16-2004
<(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed)...
> Hello,
>
> I have a question about passing an object into a constructor.
>
> How can I check to make sure that the object is good/valid that is
> being passed into the constructor.


Why would you like to do that?
IMHO the right way to do it is that in the documentation of the class say
something as:

Car::Car( Engine* engine )
Precondition: engine points to a valid Engine object.

and leave the responsibility for this to the caller. This is the approach
the standard library has.

For example:
18.6.2.3 set_unexpected
unexpected_handler set_unexpected(unexpected_handler f) throw();
1 Effects: Establishes the function designated by fas the current
unexpected_handler.
2 Requires: f shall not be a null pointer.

--
Dag Henriksson


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      01-16-2004
In article <(E-Mail Removed)>,
(E-Mail Removed) wrote:

> Hello,
>
> I have a question about passing an object into a constructor.
>
> How can I check to make sure that the object is good/valid that is
> being passed into the constructor.
>
> Obviously I could check to see if it is non-NULL, but that still will
> not necessarily tell me anything.
>
> For example:
>
> /* here is a constructor car that takes an engine class as an
> argument. But, I better check to make sure that engine is valid
> so how do I do that?
> */
> Car::Car( Engine* engine )
> {
>
> // check to make sure that engine is valid.
>
>
> }


In other words, you want to guard against someone doing something like:

Car myCar( (Engine*) 0x543562 );

(where 0x543562 doesn't point to an actual Engine object.) Or some such?

There isn't much you can do about this, but here is one idea:

class Engine {
static std::set< Engine* > allEngines;
public:
static bool isValid( Engine* anEngine ) {
return allEngines.count( anEngine ) == 1;
}

Engine() {
allEngines.insert( this );
}
~Engine() {
allEngines.erase( this );
}
};

Now:

Car::Car( Engine* engine ) {
assert( Engine::isValid( engine ) );
//...
}

This is not a cheep operation! It will probably slow your program down
tremondusly if you do this with every class. Be sure to make it so you
can define the extra code out when you build a release version.
 
Reply With Quote
 
Dag Henriksson
Guest
Posts: n/a
 
      01-16-2004

"Daniel T." <(E-Mail Removed)> skrev i meddelandet
news(E-Mail Removed)...
> In other words, you want to guard against someone doing something like:
>
> Car myCar( (Engine*) 0x543562 );
>
> (where 0x543562 doesn't point to an actual Engine object.) Or some such?
>
> There isn't much you can do about this, but here is one idea:


Your idea will probably work on some implementation, but technically is the
effect of using an invalid pointer value undefined and could cause a
system-generated runtime fault.

That means that you can not check if a pointer argument is valid or not
inside a function (or anywhere else). When the pointer is passed to the
function (by value) we have already invoked undefined behavior.

--
Dag Henriksson


 
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
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments