Returning a concrete class via abstract class API

Hi there,

I have to return a concrete class instance through a abstract class function.
I have the code as given below.  I want to associate the returnConcrete( ) function with the
IAbstract class somehow... I am not sure if this is possible.
I want to be able to use it in Test.cpp something like

IAbstract * c = IAbstract::returnConcrete( )

Is it possible?
Thanks,

1:
In IAbstract.h
--------------------
class IAbstract
{
   public:
   virtual void somePureVirtualFunc() = 0;

};
IAbstarct * returnConcrete();    <----

In Concrete.h
-----------------
class Concrete : public IAbstrct
{

};

Test.cpp
--------

IAbsract * c = returnConcrete();

Open in new window

ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Sure, that is not only possible, that's the usual way to do that, i.e.
class IAbstract
{
   public:
   virtual void somePureVirtualFunc() = 0;

};
IAbstarct * returnConcrete();   



class Concrete : public IAbstrct
{

};

IAbstarct * returnConcrete() {

  return new Concrete; // <----- that's all
}

// ...

IAbsract * c = returnConcrete();

Open in new window

0
ambuliAuthor Commented:
Thanks JKR.  yes, that is how I have it.  But, I am thinking that the returnConcrete( ) function when used in Test.cpp should have a scope( what I mean is it appears returnConcrete( ) is a global function when doing it this way.  I want to know if it is possible to have a static method in the IAbstract class itself to return the concrete class instance.

0
jkrCommented:
Well, that defies the purpose - just think about it, you are defiving from 'IAbstract' and are about to create a static method that uses 'new' for a derived class. Yet it *does* work from a technical point of view:
class IAbstract
{
   public:
   virtual void somePureVirtualFunc() = 0;

   static IAbstract * returnConcrete();   
};




class Concrete : public IAbstract
{

   virtual void somePureVirtualFunc() {};
};

IAbstract * IAbstract::returnConcrete() {

  return new Concrete;
}

// ...

void main () {

  IAbstract * c = IAbstract::returnConcrete();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.