Boost logo

Boost Users :

From: Windflower Waters (WWaters_at_[hidden])
Date: 2007-05-01 10:08:55


This is what I would do:

Main()
{
  shared_ptr<B> poly(new B); // Start with a pointer that matches the
type

  MyFunction( dynamic_pointer_cast<A>(poly) ); // Cast it in the
argument list

}

-- Windflower

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Hughes, James
Sent: Tuesday, May 01, 2007 4:19 AM
To: Boost-users_at_[hidden]
Subject: [Boost-users] [Shared_ptrs] As I doing this right

Although I have been using shared_ptr's for a while, I still get a bit
confused with polymorphic stuff ...no change there.

An pseudocode example:

Class A
{
};

Class B : public A
{
 DoStuff1();
 DoStuff2();
 DoStuff3();
 DoStuff4()
};

// MyFunction handles A and classes derived from A
MyFunction(shared_ptr<A>)
{
};

Main()
{
  shared_ptr<A> poly<new B); // OK so far

  MyFunction(poly); // Still OK -

  // OK, done this, but now if I want to call a load of stuff in an
instance of B
  // I either have to make a load of pointer casts (because poly is an
A)

  dynamic_pointer_cast<B>(poly)->DoStuff1();
  dynamic_pointer_cast<B>(poly)->DoStuff2();
  dynamic_pointer_cast<B>(poly)->DoStuff3();
  dynamic_pointer_cast<B>(poly)->DoStuff4();

  // Or this?

  shared_ptr<B> tmp = dynamic_pointer_cast<B>(poly); // is this right?
        
// I have to create a new pointer?

  tmp->DoStuff1();
  tmp->DoStuff2();
  tmp->DoStuff3();
  tmp->DoStuff4();
  

}

So, the quesiton is one of efficiency - if I was using pointers, I would
just new a B and assign to a B* variable, then pass in to the function
which would use it as an A - no temp required. With above a new tmp
pointer seems to be required - am I right or have I missed something
blindingly obvious.

James

This message (including any attachments) contains confidential
and/or proprietary information intended only for the addressee.
Any unauthorized disclosure, copying, distribution or reliance on
the contents of this information is strictly prohibited and may
constitute a violation of law. If you are not the intended
recipient, please notify the sender immediately by responding to
this e-mail, and delete the message from your system. If you
have any questions about this e-mail please notify the sender
immediately.
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net