Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart Pointers with Abstract Factory

Reply
Thread Tools

Smart Pointers with Abstract Factory

 
 
muswei muswei is offline
Junior Member
Join Date: Dec 2008
Posts: 1
 
      12-12-2008
I am trying to create abstract factory function to create smart pointers of different derived classes that have the same base class. One of the derived classes has an extra function that is not in the base or the other derived class. If I try to call that extra function "WhoAmI" the code will not compile. Any idea what is wrong here? Do I need to cast my auto_ptr from Base to DerivedB? Is this the proper way of doing this? In the end I just want some code that will create different derived classes based on certain criteria that do not leak memory.

Here is my full code that I am trying to get to work.

Code:
#include <iostream>
#include <fstream>

#include <string>

#include <stdio.h>
#include <sstream>
#include <stdlib.h>
#include <sstream>
#include <memory>



using namespace std;
class Base
{
public:
	virtual void PrintMe() = 0;
};

class DeriveA : virtual public Base
{
public:
	void PrintMe()
	{
		std::cout << "I am DeriveA\n";
	}
};

class DeriveB : virtual public Base
{
public:
	void PrintMe()
	{
		std::cout << "I am DeriveB\n";
	}

	void WhoAmI()
	{
		std::cout << "I think I am DeriveB\n";
	}
};

std::auto_ptr<Base> CreateIt(std::string t)
{
	if(t == "A")
	{
		std::auto_ptr<Base> p(new DeriveA()); 
		return p;
	}

	if(t == "B")
	{
		std::auto_ptr<Base> p(new DeriveB());
		return p;
	}
}

int main(int argc, char* argv[])
{
	std::auto_ptr<Base> p = CreateIt("A");
	p->PrintMe();

	std::auto_ptr<Base> c = CreateIt("B");
	c->PrintMe();
	c->WhoAmI();

	return 0;
}
Thanks
 
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
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
ABC interfaces, smart pointers and factory functions Steven T. Hatton C++ 7 11-30-2006 10:45 AM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments