tags:

views:

161

answers:

6

Can I create a function inside a class without defining it in the header file of that class?

+4  A: 

Why don't you try and see?


[˙ʇ,uɐɔ noʎ 'oᴎ]

Update: Just to reflect on the comments below, with the emphasis of the C++ language on smart compiling, the compiler needs to know the size of the class (thus requiring declaration of all member data) and the class interface (thus requiring all functions and types declaration).

If you want the flexibility of adding functions to the class without the need to change the class header then consider using the pimpl idiom. This will, however, cost you the extra dereference for each call or use of the function or data you added. There are various common reasons for implementing the pimpl:

  • to reduce compilation time, as this allows you to change the class without changing all the compilation units that depend on it (#include it)
  • to reduce coupling between a class' dependents and some often-changing implementation details of the class.
  • as Noah Roberts mentioned below the pimpl can also solve exception safety issues.
wilhelmtell
I know it doesn't work , but I'm asking if it is possible?
Nick Brooks
lol ok, no, you can't. You have to declare everything you have in a class so that the compiler knows the size of the class and what interface to expose to anyone who #includes the header.
wilhelmtell
... Then again you have the bridge pattern and the pimpl idiom, which essentially boil down to having a member pointer that points to "stuff". But that still makes the compiler happy because it still knows the size of the class.
wilhelmtell
How many other people copy-pasted [ʇ,uɐɔ noʎ 'oᴎ] in to a text editor to figure out how WilhelmTell of Purple-Magenta did that?
John Dibling
actually, one of the best reasons for using pimpl is exception safety.
Noah Roberts
A: 

No, you can't. It wouldn't make much sense anyway. How should users of your class that include the header file know about those functions and use them?

pmr
:( But I can't be bothered to do so ...
Nick Brooks
It doesn't matter that you don't care. The compiler does. The compiler needs to know "stuff", usually for enforcing type-safety or for deducing necessary information (class size etc.), but sometimes because the standard said so -- for optimization purposes or for whatever the standard had in mind.
wilhelmtell
+3  A: 

No. However, you can mimic such:


struct X
{
  void f();

  X();
  ~X();

private:
  struct impl;
  impl * pimpl;
};

// X.cpp
struct X::impl
{
  void f()
  {
    private_function();
    ...
  }
  void private_function() { ...access private variables... }

};

//todo: constructor/destructor...

void X::f() { pimpl->f(); }

Noah Roberts
+2  A: 

Short answer: No, you can't.

However, if you're trying to inject a private function into the class that will only be used in that class's implementation, you can create a function in an anonymous namespace within that class's .cpp file that takes an object of that type by reference or pointer.

Note that you won't be able to muck with the passed objects internal state directly (since there's no way for the class to declare friendship with that anonymous function), but if the function just aggregates operations from the public interface of the class it should work just fine.

Drew Hall
C++ is so much harder than C#
Nick Brooks
It's more that C++ allows you to do much naughtier things.
DoctorT
A: 

I have no idea what You are trying to do, but I have a strange gut feeling, that Pointer To Implementation (pImpl) idiom might be helpful. If you want to add a public method in a cpp file and that method is not declared in the class definition in a header, You can't do that and it doesn't make sense.

Maciej Hehl
A: 

Why would you? To what end?

If you tell us what you want to accomplish, you may get answers that could help!

struppi