Archived

This topic is now archived and is closed to further replies.

Russell

Classes: inline implementation or source file?

Recommended Posts

I once heard that there was an advantage to implementing the entire class within the header file, rather than declaring it in the header file and implementing it in a seperate source file. For example:
  
// myclass.h

// This is implemented in the header file.


class MyClass {
  public:
    MyClass () {
        _data = 0;
    }
    int GetData () {
        return _data;
    }
    void SetData (int data) {
        _data = data;
    }
  private:
    int _data;
};
  
The other method is implementing in a seperate source file, so we would have:
  
// myclass.h

// No implementation here.


class MyClass {
  public:
    MyClass ();
    int GetData ();
    void SetData;
  private:
    int _data;
};
  

  
// myclass.cpp

// This is implemented in the source file


MyClass::MyClass () {
    _data = 0;
}
int MyClass::GetData () {
    return _data;
}
void MyClass::SetData (int data) {
    _data = data;
}
  
I know that the member functions get inlined if they are implemented in the header file, but that isn''t the reason someone recommended implmementing inline in the header file. I believe the person who told me this said he got it from The C++ Programming Language by Stroustrop, so maybe that will ring a bell. Someone did point out that if you implement everything in the header file, it has to recompile everything that uses the class each time you make a change to that class, where you would only have to recompile one method if you had implemented in the source file. So, are there any reasons to choose one method over the other? Or is this just yet another "matter of taste" issues? Thanks, Russell

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think inline implentation in better, but plz store ur class interface in the header file. Anyway, Inline implentation is suitable for simple class function.But if the class function is so complicated, i recommend u seperate the implentation and the interface.

Share this post


Link to post
Share on other sites
The reason you''d want to stick an inline implementation in a header file is for a certain braindead compiler that refuses to inline things unless they''re in the header. Damn you, Gates. Damn you.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
The reason you'd want to stick an inline implementation in a header file is for a certain braindead compiler that refuses to inline things unless they're in the header. Damn you, Gates. Damn you.


Don't listen to me. I've had too much coffee.


Eh? (for once) I don't think this is a ms specific problem. inline functions must ALWAYS be included rather then compiled in, because the compiler places the instructions in the calling function rather then linking them in.

Either that or I'm way off on this one

Anyway, from an OOP point of view having your entire implementation in the header is bad 'cos you are showing your implementation to clients, who (theoretically) should be bliind to it. I think the java way of having a simple program that lists public members from a class is way better then C++'s way of exposing every member.


[edited by - ChaosEngine on November 4, 2002 1:12:27 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Russell
I know that the member functions get inlined if they are implemented in the header file...

Nope. Member functions get inlined if they are defined within the class declaration.

quote:
So, are there any reasons to choose one method over the other? Or is this just yet another "matter of taste" issues?

It really doesn''t matter to the compiler; headers are always inserted into the sourcefile before compilation anyway. Organize your code in the way that is most logical and comprehensible to you, and achieves the results you desire. You can always release binary code (objects/libs/DLLs) with minimal headers to expose public interfaces only.

C++''s build process needs a massive overhaul.

Share this post


Link to post
Share on other sites
quote:
Original post by ChaosEngine
I think the java way of having a simple program that lists public members from a class is way better then C++'s way of exposing every member.



Hmm.. Why would you care if it exposes all members to clients? They can't use the non-public members anyway, even if they know what they're called exactly. As long as you const your members and return const references, you should be fine.

[edited by - Bas Paap on November 4, 2002 3:13:18 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Bas Paap
Hmm.. Why would you care if it exposes all members to clients? They can''t use the non-public members anyway, even if they know what they''re called exactly. As long as you const your members and return const references, you should be fine.

Besides, you can always write a separate (set of) header file(s) that only list public members. Nit.

Share this post


Link to post
Share on other sites
quote:
Original post by Bas Paap
[quote]Original post by ChaosEngine
I think the java way of having a simple program that lists public members from a class is way better then C++''s way of exposing every member.



Hmm.. Why would you care if it exposes all members to clients? They can''t use the non-public members anyway, even if they know what they''re called exactly. As long as you const your members and return const references, you should be fine.

[edited by - Bas Paap on November 4, 2002 3:13:18 AM]

not true. you can get at private members of a class if you really want to. I had some idiot I was working cast a pointer to my class so they could access private members directly.

quote:

Besides, you can always write a separate (set of) header file(s) that only list public members. Nit.


that''s a pain in the ass.

I was merely pointing out that the C++ header file is not the "interface" to the class. It is the definition of the class which includes the interface.

Share this post


Link to post
Share on other sites