Results 1 to 1 of 1

Thread: Adding member to existing class?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Question Adding member to existing class?

    Hello all.

    I have experience in coding VB .NET, but I recently jumped over to c++.
    I read lots of tutorials, and know all basics of making classes, templates, functions, variables, references, and so forth.

    I know how a class can inherit from other classes, and can this way add attributes to it's own. My question is; how can I add members to an already defined class that is outside of my project?

    For example, I want to add the "ToString" function to the "int" class so I can easily format strings with numbers. I tried using the following code, but it gave a couple of compilation errors (... not defined):
    Code:
    string int::ToString()
    {	
    	stringstream ss;
    	ss << this;
    	return ss.str();	
    }
    My guess is that I somehow need to re-open the int class and add the function in there, but I haven't seen good examples on the net how to do just that. (if that is even possible)

    Is it possible to add members to existing classes, and if so, how?

    EDIT

    It could be it is not possible for int, since it is a system type. But what about other classes like "myclass"?
    Last edited by bergerkiller; Nov 22nd, 2010 at 12:55 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width