Add a Method to Existing C++ Class in Other File

Add a method to existing C++ class in other file

i found out that c++ is better at doing this than obj-c.

i tried the following and it works great!

the key is, enclose all of your classes in a namespace and then extend your target classes there with the same class name.

//Abc.h
namespace LibraryA {
class Abc { //This class is from a 3rd party library....
// ...I don't want to edit its source file.
void methodOne();
void methodTwo();

}
}

//Abc+Ext.hpp
namespace MyProj {
class Abc : public LibraryA::Abc {
using Base = LibraryA::Abc; //desc: this is to easily access the original class...
// ...by using code: Abc::Base::someOrigMethod();
using Base::Base; //desc: inherit all constructors.

protected:
//---added members:
int memberAdded;

public:
//---added methods:
void methodAdded();

//---modified virtual member funcs from original class.
void origMethod_A() override;

}
}

//Abc+Ext.cpp
namespace MyProj {
void Abc::origMethod_A() {
//...some code here...
Base::origMethod_A(); //desc: you can still call the orignal method
//...some code here...
}
}

//SomeSourceFile_ThatUses_Abc.cpp
namespace MyProj { //IMPT NOTE: you really need to enclose your...
// ...project specific code to a namespace so you can...
// ...use the version of class Abc you extended.


void SomeClass::SampleFunc(){
Abc objX; //create MyProj::Abc object.
objX.methodAdded(); //calls MyProj::Abc::methodAdded();
objX.origMethod_A(); //calls MyProj::Abc::origMethod_A();

Abc::Base objY; //create Library::Abc object.
//objY.methodAdded(); //method not existing.
objY.origMethod_A(); //calls Library::Abc::origMethod_A();

//...some code here...
}

}

//SomeModule.cpp
namespace OtherNamespace {
void SomeOtherClass::SampleOtherFunc(){
Abc objZ; //create Library::Abc object.
//objZ.methodAdded(); //method not existing.
objZ.origMethod_A(); //calls LibraryA::Abc::origMethod_A();
}

}

you can even extend class Abc differently within other module namespaces.

//MyLib_ModuleA_Classes.hpp
namespace MyLib_ModuleA {
class Abc : public LibraryA::Abc {
//...add extensions here...
void addedMethod_X();
void origMethod_A() override; //own overriden behavior specific to this ModuleA only.
}

}

//MyLib_ModuleB_Classes.hpp
namespace MyLib_ModuleB {
class Abc : public LibraryA::Abc {
//...add extensions here...
void addedMethod_Y();
void origMethod_A() override; //own overriden behavior specific to this ModuleB only.
}

}

if in case class Abc is in global namespace, though i haven't tried it yet, i think you can just replace LibaryA::Abc to ::Abc.

sorry for the very late answer i've been doing this approach for around 4 years now and it's structure is very well useful.
i tried this in c++14 but i think this is still doable in c++11. now i used c++17 and it compiles fine. i'm planning to convert to c++20
when the compilers i used already completed c++20 features.

Adding methods into created class C++

There is no way to add a new method to an existing class without changing this class.

If you want to work with an enriched class adding just new methods method, all you need to do is:

class MyEnrichedClass : public MyClass {
public:
MyEnrichedClass (int ID) : MyClass(ID) {}
void myNewFantasticMethod (...) { ... }
};

In your added methods you have access to all public and protected members of your base class (but not the private ones).

If you know in advance that you need a method with a well defined signature and for a particular purpose, but you want to define it dynamically, you could have a function object as member and have your method call this function object (or function pointer if you prefer). But your dynamic function would not have access neither to private nor to protected members.

C++/Any OOP language: Add a method to existing class without in-class declaration

In C# a class can be spanned over multiple files. This feature is called partial classes. You can use partial classes to:

  • Work at the same class with multiple devs without having to merge the class file all the time
  • You can add additional code to auto-generated classes which you might want to regenerate at a later time

Sample (taken from MSDN)

public partial class Employee
{
public void DoWork()
{
}
}

public partial class Employee
{
public void GoToLunch()
{
}
}

All parts of the class need to be flagged with the partial keyword. This means that you can only expand those classes that their authors intended to be expanded. This makes sure that the original developer keeps control over what can be done with his class (like abstract, sealed, ...)

Implement functions from other class files C++

Your splitting of code into several files is unfortunate. You should put the declaration of types and functions you wish to use in more than one file into a header file. Therefore, create the file enemy.hpp and put enemy's declaration in it:

class enemy
{
public:
void genRandEnemy();
};

Put the definition of functions into a separate source file and name it enemy.cpp. It will look like this:

#include "enemy.hpp"
#include <iostream>
// All your other includes needed to define the function...

void
enemy::genRandEnemy()
{
// Implementation of the function...
}

Then, in any file that needs to call enemy::genRandEnemy:

#include "enemy.hpp"

void
f()
{
enemy foe;
foe.genRandEnemy();
// Or whatever...
}

Note that your code is inconsistent: You declare genRandEnemy as a member function of class enemy but define it as a free function outside any class. My example always puts it as a member of enemy. (This actually seems weired to me. A function with that name would be supposed to create a new enemy and return it. If it is a member of enemy, I will need an enemy ahead of time to create one.)

Two other remarks:

  • You are re-seeding the random generator every time the genRandEnemy function is called. This will make for a poor randomness.
  • I'm sure you can come up with a better solution than the pile of if(randEnemy == 1) { cout << enemy[1]; }. Can you?

Add method to a class from another project

The thing You are looking for is called Extension method:

  • DotNetPerls
  • MSDN link
  • What are Extension Methods?

Code for base class:

namespace ProjectLibrary
{
public ClassA
{
// some methods
}
}

Extension method:

using ProjectLibrary;
namespace ProjectOnPC
{
void SaveToFile(this ClassA Obj, string Path)
{
//Implementation of saving Obj to Path
}
}

Calling in Your program:

using ProjectLibrary;
using ProjectOnPc; //if You won't include this, You cannot use ext. method

public void Main(string[] args)
{
ClassA mObj = new ClassA();
mObj.SaveToFile("c:\\MyFile.xml");
}

Regarding C++ Include another class

What is the basic problem in your code?

Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).

The compiler needs to see the composition of a type when you write something like

ClassTwo obj;

This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo. The most common way to do this in C++ is to split your code in to header files and source files.

The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who's object they create.

Why can't I simply put all code in cpp files and include them in other files?

You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

How to solve this particular problem?

Your code should be organized as follows:

//File1.h

Define ClassOne 

//File2.h

#include <iostream>
#include <string>

class ClassTwo
{
private:
string myType;
public:
void setType(string);
std::string getType();
};

//File1.cpp

#include"File1.h"

Implementation of ClassOne

//File2.cpp

#include"File2.h"

void ClassTwo::setType(std::string sType)
{
myType = sType;
}

void ClassTwo::getType(float fVal)
{
return myType;
}

//main.cpp

#include <iostream>
#include <string>
#include "file1.h"
#include "file2.h"
using namespace std;

int main()
{

ClassOne cone;
ClassTwo ctwo;

//some codes
}

Is there any alternative means rather than including header files?

If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

Add methods which return same class objects to a existing class in C++

You can add your own one_element() wrapper along with a c_Aext constructor that'll take a c_A:

class c_Aext : public c_A { 
public:
c_Aext() : c_A() { } // just call parent's default
c_Aext(const c_A c) : c_A(c) { } // call parent's copy

c_Aext one_element() { return c_Aext(c_A::one_element()); }
};

Full compiled example here: https://ideone.com/auSoH4

c++ adding method to class defined in header file

No, but you could add a method that takes a reference/pointer to a CFun class - you just won't have access to private data:

void Hello(CFun &fun)
{
cout << "hello" << endl;
}

This is probably the best you'll be able to do. As pointed out by litb - this function has to be in the same namespace as CFun. Fortunately, namespaces, unlike classes, can be added to in multiple places.



Related Topics



Leave a reply



Submit