Sign in to follow this  
AcidZombie24

extend a class using public members?

Recommended Posts

I just learned the + operator can be global and can have the left hand side allowing the programmer to do this
class MyClass
{
	int val;
public:
	explicit MyClass (int v) : val(v) { }
	int operator += (const MyClass& v) { return val += v.val; }
	int doSomething(int a, int b) { return b; }
};

int operator + (const MyClass& lhs, const MyClass& rhs)
{
	MyClass temp(lhs);
	return temp += rhs;
}

void func()
{
	MyClass a(5), b(9);
	int ret = a + b;
}


Now something i always wanted to do was extend a class using only its public members. Is there a way i can make the below possible?
class MyClass
{
	int val;
public:
	explicit MyClass (int v) : val(v) { }
	int operator += (const MyClass& v) { return val += v.val; }
	int doSomething(int a, int b) { return b; } //<----- added
};

int operator + (const MyClass& lhs, const MyClass& rhs)
{
	MyClass temp(lhs);
	return temp += rhs;
}

//I want to make a util function for the class. In this case, default the b param
//i dont want to be able to access private or protected members.
int doSomething(MyClass &lhs, int a) 
{
	return lhs.doSomething(a, 5);
}

void func()
{
	MyClass a(5), b(9);
	int ret = a + b;
	a.doSomething(20); //<-- then i can use my new function for the class as if it was written as part of the class. Its very lame to do
	doSomething(a, 20); //<-- it doesnt have the same feel.
}


Share this post


Link to post
Share on other sites
It seems you want to "force" the function that extends the class to only use the public members of the class, right?

In C++ classes can be extended by using inheritance, so I'd recommend reading about inheritance, but don't rush to it. Here's an OK text about inheritance:
http://www.codersource.net/cpp_tutorial_inheritance.html

Specifically, you could create your "MyClass" class with a private variable and that defines the "operator +=", then create a class that inherits from "MyClass" and defines "doSomething". Since the variable was private, the class that inherits can only access the constructor and "operator +=" in "doSomething".

"MyClass" would be called a base class, and the class that inherits would be called a derived class, and you might find the following point useful:
Quote:
http://www.codersource.net/cpp_tutorial_inheritance.html
There are some points to be remembered about C++ inheritance. The protected and public variables or members of the base class are all accessible in the derived class. But a private member variable not accessible by a derived class.

Share this post


Link to post
Share on other sites
C# 3.0 can do that - http://msdn.microsoft.com/en-us/library/bb383977.aspx

namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}

...

string s = "Hello Extension Methods";
int i = s.WordCount();

Share this post


Link to post
Share on other sites
No, there's no way to do that in C++. But you should prefer free functions to member functions, anyway. If the function doesn't need to access the class's innards (private/protected data), or can be implemented in terms of its public interface, a free function is usually a better option.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this