Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C's category-like construct or technique in C++?

Tags:

c++

categories

Objective-C category feature allows programmer to add new method which was not defined in original class definition.

Can I archive similar functionality (language construct or some technique) on C++?

Major concern is consistent method calling syntax (. or -> operator).

like image 872
eonil Avatar asked Feb 19 '13 07:02

eonil


People also ask

What are Objective-C categories?

Categories provide the ability to add functionality to an object without subclassing or changing the actual object. A handy tool, they are often used to add methods to existing classes, such as NSString or your own custom objects.

What is Objective-C based on?

The Objective-C model of object-oriented programming is based on message passing to object instances. In Objective-C one does not call a method; one sends a message. This is unlike the Simula-style programming model used by C++.

Can you write C in Objective-C?

You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.

Is C and Objective-C the same thing?

The main difference in C and Objective C is that C is a procedure programming language which doesn't support the concepts of objects and classes and Objective C is Object-oriented language which contains the concept of both procedural and object-oriented programming languages.


1 Answers

Let's consider the following class to be extended:

struct A {
    int x, y;
    A(int x, int y) : x(x), y(y) {}
};

You can inherit from this class or write a wrapper class which contains an instance of this class. In most cases, inheritance is the way to go, as a wrapper class isn't an A but it wraps (contains) an A.

With C++11 move semantics, promoting an instance A to a subclass B (inheriting A) will be efficient and doesn't require to copy the instance A:

class B : public A {
public:
    B (A &&a) : A(a), someOtherMember(a.x + a.y) {}

    // added public stuff:
    int someOtherFunction() const { return someOtherMember; }

private:
    // added private stuff:
    int someOtherMember;
};

Full code with example: http://ideone.com/mZLLEu

Of course the function I added is a bit silly (and the member even more, since it doesn't respect further changes of the original members x and y), but you should get an idea of what I want to demonstrate.

Note the constructor B (A &&a) which is something I call "promote constructor" (this is not a standard term). Normally, B (B &&b) is a move constructor, which moves the contents of the provided B instance into a new B about to be constructed. I use the move semantics to move an instance of A (which has been returned by another function) into the super-class A of B.

Effectively, you can promote A to B while making it possible to use a B as an A.

In contrast to Soonts' answer, my solution also works with added virtual tables, since it doesn't rely on unsafe pointer casting.

like image 175
leemes Avatar answered Oct 23 '22 07:10

leemes