Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11892 > unrolled thread

.h and .cpp

Started byChristopher <cpisz@austin.rr.com>
First post2011-11-28 09:20 -0800
Last post2011-11-30 09:23 +1300
Articles 6 on this page of 46 — 17 participants

Back to article view | Back to comp.lang.c++


Contents

  .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 09:20 -0800
    Re: .h and .cpp Leigh Johnston <leigh@i42.co.uk> - 2011-11-28 17:30 +0000
      Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 10:39 -0800
        Re: .h and .cpp Tobias Müller <troplin@bluewin.ch> - 2011-11-28 19:09 +0000
    Re: .h and .cpp ralph <nt_consulting64@yahoo.net> - 2011-11-28 12:37 -0600
      Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 10:48 -0800
        Re: .h and .cpp ralph <nt_consulting64@yahoo.net> - 2011-11-28 14:24 -0600
        Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 11:24 +0000
          Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-01 21:42 +0000
            Re: .h and .cpp Rui Maciel <rui.maciel@gmail.com> - 2011-12-02 02:03 +0000
              Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-02 20:59 +0000
    Re: .h and .cpp "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-28 19:45 +0100
      Re: .h and .cpp Leigh Johnston <leigh@i42.co.uk> - 2011-11-28 19:03 +0000
        Re: .h and .cpp "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-28 20:36 +0100
      Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-28 22:52 +0000
        Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 11:39 +0000
          Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-30 12:50 +0000
            Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 13:57 +0000
              Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-30 15:06 +0000
            Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 16:21 +0000
          Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 16:12 +0000
    Re: .h and .cpp Goran <goran.pusic@gmail.com> - 2011-11-28 10:58 -0800
      Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 13:30 -0800
        Re: .h and .cpp Tobias Müller <troplin@bluewin.ch> - 2011-11-29 06:44 +0000
        Re: .h and .cpp Goran <goran.pusic@gmail.com> - 2011-11-29 01:17 -0800
        Re: .h and .cpp Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-30 00:53 -0800
      Re: .h and .cpp "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-29 10:57 +0100
      Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 12:05 +0000
    Re: .h and .cpp Rui Maciel <rui.maciel@gmail.com> - 2011-11-28 20:07 +0000
    Re: .h and .cpp Geoff <geoff@invalid.invalid> - 2011-11-28 12:17 -0800
      Re: .h and .cpp Ian Collins <ian-news@hotmail.com> - 2011-11-29 11:41 +1300
        Re: .h and .cpp gwowen <gwowen@gmail.com> - 2011-11-29 07:37 -0800
      Re: .h and .cpp Goran <goran.pusic@gmail.com> - 2011-11-29 00:08 -0800
      Re: .h and .cpp Daniel <danielaparker@gmail.com> - 2011-11-29 08:31 -0800
        Re: .h and .cpp ralph <nt_consulting64@yahoo.net> - 2011-11-29 14:49 -0600
          Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 00:21 +0000
    Re: .h and .cpp Andy Champ <no.way@nospam.invalid> - 2011-11-28 21:30 +0000
    Re: .h and .cpp Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-28 18:05 -0600
    Re: .h and .cpp Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-28 18:27 -0600
    Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-29 07:55 +0000
      Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-29 12:15 -0800
        Re: .h and .cpp Leigh Johnston <leigh@i42.co.uk> - 2011-11-29 20:19 +0000
          Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-29 12:31 -0800
            Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 00:31 +0000
          Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-30 09:43 +0000
        Re: .h and .cpp Ian Collins <ian-news@hotmail.com> - 2011-11-30 09:23 +1300

Page 3 of 3 — ← Prev page 1 2 [3]


#11958

FromChristopher <cpisz@austin.rr.com>
Date2011-11-29 12:15 -0800
Message-ID<bcedd305-cc5a-49b3-b038-1144e62a7076@p16g2000yqd.googlegroups.com>
In reply to#11933
On Nov 29, 1:55 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> > 1b) A .h file should never contain class implementation (except for
> > inline functions or methods)
>
>   That's a null statement. If a header file contains implementations,
> they will be inline by necessity (else you'll get linker errors if the
> header is included more than once), so that rule isn't really saying
> anything.


I've seen this claim in several of the replies. I made a minimal
example that compiles and links in Visual Studio 2008 SP1 (what they
use at work).

--ClassA.h---
#ifndef CLASSA_H
#define CLASSA_H

#include <iostream>

class A
{
public:

    A()
    :
        m_x(0)
    {
        std::cout << "Contructor" << std::endl;
    }

    ~A();

    int Method1(const int y)
    {
        m_x = 4 * y;
        return m_x;
    }

    int Method2(const int y);

private:

    int m_x;
};

#endif

--ClassA.cpp---

#include "ClassA.h"

A::~A()
{
}

int A::Method2(const int y)
{
    m_x = 2 * y;
    return m_x;
}


--main.cpp---

#include "ClassA.h"


int main()
{
    A a;
    std::cout << a.Method1(5) << std::endl;
    std::cout << a.Method2(5) << std::endl;
}

--dummy.cpp--
#include "ClassA.h"

void Foo()
{
    for(int x = 0; x < 10; ++x)
    {
        A a;
        std::cout << "I constructed an A " << x << "times." <<
std::endl;
    }
}

------

ClassA.h contains implementation of the A class. So does ClassA.cpp.
ClassA.h is included in more than one compilation unit: ClassA.cpp,
main.cpp, and dummy.cpp
No linker errors.

Yet, this is an example of what I am talking about that should not be
done. The constructor and Method1 are not inline, and have no business
being implemented in the header. They should be in the .cpp, unless
they were inlined. Keep in mind these are simple for example purposes.




[toc] | [prev] | [next] | [standalone]


#11959

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-11-29 20:19 +0000
Message-ID<3-mdnWHDPLjwo0jTnZ2dnUVZ7o-dnZ2d@giganews.com>
In reply to#11958
On 29/11/2011 20:15, Christopher wrote:
> On Nov 29, 1:55 am, Juha Nieminen<nos...@thanks.invalid>  wrote:
>>> 1b) A .h file should never contain class implementation (except for
>>> inline functions or methods)
>>
>>    That's a null statement. If a header file contains implementations,
>> they will be inline by necessity (else you'll get linker errors if the
>> header is included more than once), so that rule isn't really saying
>> anything.
>
>
> I've seen this claim in several of the replies. I made a minimal
> example that compiles and links in Visual Studio 2008 SP1 (what they
> use at work).
>
> --ClassA.h---
> #ifndef CLASSA_H
> #define CLASSA_H
>
> #include<iostream>
>
> class A
> {
> public:
>
>      A()
>      :
>          m_x(0)
>      {
>          std::cout<<  "Contructor"<<  std::endl;
>      }
>
>      ~A();
>
>      int Method1(const int y)
>      {
>          m_x = 4 * y;
>          return m_x;
>      }
>
>      int Method2(const int y);
>
> private:
>
>      int m_x;
> };
>
> #endif
>
> --ClassA.cpp---
>
> #include "ClassA.h"
>
> A::~A()
> {
> }
>
> int A::Method2(const int y)
> {
>      m_x = 2 * y;
>      return m_x;
> }
>
>
> --main.cpp---
>
> #include "ClassA.h"
>
>
> int main()
> {
>      A a;
>      std::cout<<  a.Method1(5)<<  std::endl;
>      std::cout<<  a.Method2(5)<<  std::endl;
> }
>
> --dummy.cpp--
> #include "ClassA.h"
>
> void Foo()
> {
>      for(int x = 0; x<  10; ++x)
>      {
>          A a;
>          std::cout<<  "I constructed an A "<<  x<<  "times."<<
> std::endl;
>      }
> }
>
> ------
>
> ClassA.h contains implementation of the A class. So does ClassA.cpp.
> ClassA.h is included in more than one compilation unit: ClassA.cpp,
> main.cpp, and dummy.cpp
> No linker errors.

Yes no linker errors because Method1 is inline; try engaging your brain. 
  The only non-inline functions you can have in header files are templates.

/Leigh

[toc] | [prev] | [next] | [standalone]


#11961

FromChristopher <cpisz@austin.rr.com>
Date2011-11-29 12:31 -0800
Message-ID<503886ce-b986-443c-b82d-dff409b9c447@i8g2000vbh.googlegroups.com>
In reply to#11959
On Nov 29, 2:19 pm, Leigh Johnston <le...@i42.co.uk> wrote:
> Yes no linker errors because Method1 is inline;
>
> /Leigh-

I'm using it. I was not aware it could be inline without explicitly
using the inline keyword.
I just read the faq 9.6 after your response. Learn something new
everyday.

Ok, so I should instead argue that the "best practices" in the FAQ are
followed, which state that the method should be defined outside the
class body, using the inline keywork. It would make the inlining
obvious and keep the spirit of the class defintion telling us what the
class offers instead of how. Furthermore, there should be a clear
advantage in inline the method for it to be inlined. 60,000 line
functions that spawn threads are probably not good canidates for
inlining.

[toc] | [prev] | [next] | [standalone]


#11964

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-11-30 00:31 +0000
Message-ID<slrnjdaua4.1bk.grahn+nntp@frailea.sa.invalid>
In reply to#11961
On Tue, 2011-11-29, Christopher wrote:
> On Nov 29, 2:19 pm, Leigh Johnston <le...@i42.co.uk> wrote:
>> Yes no linker errors because Method1 is inline;
>>
>> /Leigh-
>
> I'm using it. I was not aware it could be inline without explicitly
> using the inline keyword.
> I just read the faq 9.6 after your response. Learn something new
> everyday.
>
> Ok, so I should instead argue that the "best practices" in the FAQ are
> followed, which state that the method should be defined outside the
> class body, using the inline keywork. It would make the inlining
> obvious and keep the spirit of the class defintion telling us what the
> class offers instead of how.

Well, the class definition already tells us a lot about the "how" --
the private section.  I happily put trivial definitions in the class
itself, if it doesn't /overshadow/ the "this is the interface" view of
the class.

Besides, it's not as if defining the inline functions after the class
doesn't obscure anything else. I frequently have a number of "loose"
functions there which are just as important as the contents of the
class itself.  It's a tradeoff, unfortunately.

> Furthermore, there should be a clear
> advantage in inline the method for it to be inlined. 60,000 line
> functions that spawn threads are probably not good canidates for
> inlining.

Better example: a function which compiles into a lot of object code,
and is called from many places.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#11972

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-11-30 09:43 +0000
Message-ID<4ed5fad6$0$3105$7b1e8fa0@news.nbl.fi>
In reply to#11959
Leigh Johnston <leigh@i42.co.uk> wrote:
>  The only non-inline functions you can have in header files are templates.

  Template function definitions behave exactly like inline function
definitions (in that they can be included in more than one compilation
unit and, if no literal inlining happens, the linker will merge all the
instances into one).

  I have heard rumors that there are some technical differences between
(non-inline) template functions and inline functions, but I really don't
know what those differences might be.

[toc] | [prev] | [next] | [standalone]


#11960

FromIan Collins <ian-news@hotmail.com>
Date2011-11-30 09:23 +1300
Message-ID<9jkt95F4d0U34@mid.individual.net>
In reply to#11958
On 11/30/11 09:15 AM, Christopher wrote:
> On Nov 29, 1:55 am, Juha Nieminen<nos...@thanks.invalid>  wrote:
>>> 1b) A .h file should never contain class implementation (except for
>>> inline functions or methods)
>>
>>    That's a null statement. If a header file contains implementations,
>> they will be inline by necessity (else you'll get linker errors if the
>> header is included more than once), so that rule isn't really saying
>> anything.
>
>
> I've seen this claim in several of the replies. I made a minimal
> example that compiles and links in Visual Studio 2008 SP1 (what they
> use at work).
>
> --ClassA.h---
> #ifndef CLASSA_H
> #define CLASSA_H
>
> #include<iostream>
>
> class A
> {
> public:
>
>      A()
>      :
>          m_x(0)
>      {
>          std::cout<<  "Contructor"<<  std::endl;
>      }

This is an inline function.

-- 
Ian Collins

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c++


csiph-web