Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello Friends,


I have a problem to integrate new feature in my product, framework team providing us some new APIs and we have to integrate in our application without impacting our old code.

Following is my question:

there is some 3rdParty classes

These classes are provided by 3rdparty component through DLL and these class are not abstract classes and we cannot change 3rdPart code.
C++
1. Class FirstBase                           
2. Class SecondBase : public FirstBase       
3. Class oldFeature : public FirstBase
4. Class NewFeature : public SecondBase



whatever members we were using from OldFeature class, the same member with same name are available in NewFeature class.

C++
//Old and existing one
Main()
{

//old
oldFeature x1;
x1.doSomething();

}


//New implementation which one I am going to change.

C++
Main()
{

	CString ver;
	
	if(ver == "New")
	{
	 NewFeature X1;
	}
	else
	{
         oldFeature x1;
	}

X1.doSomething();

}


So here I have a problem because there is a thousand places to change the code and I don't want to use If Else condition everywhere.
I have already tried to Google this problem but didn't get proper solution.

I got some solution from C++ object factory and I also tried but My base class is not abstract and returning object is FirstBase which cannot access NewFeature class member.
if I have to check run time type info in Main function then I should go with IfElse condition what I mentioned above.

So I don't want to apply any If-Else in main function, just want to pass version info to get the proper object.

E.g.- something like this:

(C++ object factory example)
C++
Main()
{
Newobject = MyFactory::CreateInstance("New");
Oldobject = MyFactory::CreateInstance("Old");
}


so please provide some solution and let me know if you didn't get my problem


Framed the question in code for better understanding:

C#
class bBase
{
public:
    bBase();
     ~bBase();
    virtual void method1() {};
}

class Base : public bBase{
    public:
    Base();
     ~Base();
    virtual void method2() {};
}

class NewBase : public Base{
    public:
    NewBase();
     ~NewBase();
    virtual void method3() {};
}

class A : public Base{
    public:
    A();
    ~A();

    void doSomethingA() {};
    void method2() {};
    void method1() {};
}

class B : public NewBase{
    public:
    B();
    ~B();
    void doSomethingB() {};
    void method3() {};
    void method2() {};
    void method1() {};
}


int main()
{
    bBase *bB1 = new bBase();
    bBase *a1 = new A(); // but can't access doSomethingA of A() class
    bBase *b1 = new B();

    A *a11 = new A(); // can access doSomethingA of A() class


return 0;
}


Thanks
Saqib
Posted
Updated 27-Jul-13 11:44am
v2
Comments
pasztorpisti 27-Jul-13 16:14pm    
Don't these classes have a common base class that contains all common functions as virtual functions???
saqib.akhter 27-Jul-13 16:32pm    
Yes, these classes have one base class which contains all common virtual function:

Hierarchy like this:
FirstBase <-- BaseObj1 <-- Base <-- baseVirtual

baseVirtual is the class whic contains all common virtual function
pasztorpisti 27-Jul-13 16:43pm    
Then the arrows should go the opposite direction.
saqib.akhter 27-Jul-13 16:49pm    
Sorry, it's my mistake, just giving the example.
saqib.akhter 27-Jul-13 16:35pm    
Product is very old, these codes are 14 or 15 yrs old that is the difficulty to integrate the new code

1 solution

Regardless of the actual implementation/class you instantiate put the result pointer into a base class pointer and all the general implementation independent code should work with base class pointers/references.
C++
BaseClassPointer* GetInstance()
{
   if (globalsettings == "whatever")
       return new Implementation1;
   if (globalsettings == "foo")
       return new Implementation2;
   assert(false);
   return NULL;
}

void ImplementationIndependentCode()
{
    BaseClassPointer* p = GetInstance();
    p->func1();
    p->func2();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900