![]() |
|
|
|
#1 |
|
I'd like to write all code of a class in only header file. But when
there is any static member varia ble, I have to define it in cpp file, not the header file. Otherwise, the static member variable may be duplicated because two or more cpp files may include the header. How can I solve this without the cpp file, which used to define the static member variable? Thanks! yanlinlin82@gmail.com |
|
|
|
|
#2 |
|
Posts: n/a
|
wrote:
> I'd like to write all code of a class in only header file. But when > there is any static member varia ble, I have to define it in cpp file, > not the header file. Otherwise, the static member variable may be > duplicated because two or more cpp files may include the header. > How can I solve this without the cpp file, which used to define the > static member variable? Simple. Don't use any static variables. If you can asnwer the following questions, you can answer your own: "I like walking outside when it's raining, and stay dry, but I don't want to wear any rain gear or carry an umbrella. How to accomplish my goal?" "I want to live a long life and be healthy, but I don't want to give up smoking or alcohol or drugs. How can I do that?" V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Victor Bazarov |
|
|
|
#3 |
|
Posts: n/a
|
* :
> I'd like to write all code of a class in only header file. But when > there is any static member varia ble, I have to define it in cpp file, > not the header file. Otherwise, the static member variable may be > duplicated because two or more cpp files may include the header. > How can I solve this without the cpp file, which used to define the > static member variable? The best way is to avoid static variables, except constants: using them is an all too common beginner's mistake, much like using goto. Constants are not problematic, just use 'const': const int x = 42; Here you don't need to be concerned about duplication because a namespace scope constant has internal linkage by default. That said, technical solutions to non-const static variable in headers include using local statics (wrapping in functions) and templatizing. But best, just say no: you can be nearly 100% sure that it's a mistake. Cheers, & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Alf P. Steinbach |
|
|
|
#4 |
|
Posts: n/a
|
* Victor Bazarov:
> > "I want to live a long life and be healthy, but I don't want to > give up smoking or alcohol or drugs. How can I do that?" It seems that yogurt is key. At least in Kazakhstan and places like that. Cheers, & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Alf P. Steinbach |
|
|
|
#5 |
|
Posts: n/a
|
Thanks for all your funny answers!
It sounds that I have asked for way to an unreachable thing. But I have found such requirements in real life indeed, while I also have some "wacky" habit (such as writing class implement in header file only). There is a example in Microsoft's ATL. I found class CWindow has a static member rcDefault. Microsoft use its own keyword "_declspec(selectany)" to slove this problem. I am curious if there is a common solution via C++ language itself. yanlinlin82@gmail.com |
|
|
|
#6 |
|
Posts: n/a
|
On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote:
> I'd like to write all code of a class in only header file. Why? The header file is visible by all users; the more you put in it, the more coupling you get, and the harder it is to maintain the program. -- James Kanze (GABI Software) email: Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 James Kanze |
|
|
|
#7 |
|
Posts: n/a
|
James Kanze wrote:
> On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote: >> I'd like to write all code of a class in only header file. > > Why? The header file is visible by all users; the more you put > in it, the more coupling you get, and the harder it is to > maintain the program. There may be thousands of good reasons to separate header files from implementation files, but that one, I do not consider convincing. A header is meant for the compiler only. Users refer to the documentation. They have no business looking at the header. There is no coupling coming from this (unless users prefer to rely on undocumented features, which is entirely their problem). Likewise: you don't get coupling from the standard headers should they be readable on a particular implementation. Why would you from another header? Best Kai-Uwe Bux Kai-Uwe Bux |
|
|
|
#8 |
|
Posts: n/a
|
Kai-Uwe Bux wrote:
> James Kanze wrote: > >> On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote: >>> I'd like to write all code of a class in only header file. >> Why? The header file is visible by all users; the more you put >> in it, the more coupling you get, and the harder it is to >> maintain the program. > > There may be thousands of good reasons to separate header files from > implementation files, but that one, I do not consider convincing. > This time, I agree with James. > A header is meant for the compiler only. Users refer to the documentation. > They have no business looking at the header. There is no coupling coming > from this (unless users prefer to rely on undocumented features, which is > entirely their problem). > Maybe, but headers have a strange way of finding themselves included in compilation units, the "lower level" the header, the more places both directly and indirectly it gets included. If there is code in the header and the project is under development, that code is likely to get changed, which results in all those compilation units being recompiled. > Likewise: you don't get coupling from the standard headers should they be > readable on a particular implementation. Why would you from another header? > You don't change your standard headers. -- Ian Collins. Ian Collins |
|
|
|
#9 |
|
Posts: n/a
|
Ian Collins wrote:
> Kai-Uwe Bux wrote: >> James Kanze wrote: >> >>> On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote: >>>> I'd like to write all code of a class in only header file. >>> Why? The header file is visible by all users; the more you put >>> in it, the more coupling you get, and the harder it is to >>> maintain the program. >> >> There may be thousands of good reasons to separate header files from >> implementation files, but that one, I do not consider convincing. >> > This time, I agree with James. With the recommendation, or with the reasoning? I did not dispute the former. >> A header is meant for the compiler only. Users refer to the >> documentation. They have no business looking at the header. There is no >> coupling coming from this (unless users prefer to rely on undocumented >> features, which is entirely their problem). >> > Maybe, but headers have a strange way of finding themselves included in > compilation units, the "lower level" the header, the more places both > directly and indirectly it gets included. If there is code in the > header and the project is under development, that code is likely to get > changed, which results in all those compilation units being recompiled. That is a one of the thousand other reasons I acknowledged. But maybe, I just misunderstood what James had in mind when he was saying "visible by all users". I took that to refer to human users not files that include the header. >> Likewise: you don't get coupling from the standard headers should they be >> readable on a particular implementation. Why would you from another >> header? >> > You don't change your standard headers. True. But they are visible by all users I thought, it was clear from my topic sentence that I have no problem with the recommendation per se to put the implementation into a different file. I just question the particular rationale. Best Kai-Uwe Bux Kai-Uwe Bux |
|
|
|
#10 |
|
Posts: n/a
|
James Kanze <james.ka...@gmail.com> wrote:
> On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote: > > > I'd like to write all code of a class in only header file. > > Why? The header file is visible by all users; the more you put > in it, the more coupling you get, and the harder it is to > maintain the program. > I don't think that putting all code in header file would increase the coupling. Since the implementation code is private in class, users can ignore them usually. The reason I do this is from the occasion that I publish my code to others in Windows platform. There are many compiling types for library (such as multi-threading, multi-threading DLL), and every type can build as both Debug and Release (as well as ANSI or Unicode, etc.). As a result, I have to give so many different .lib files. If I publish my .h and .cpp files, troubles also can be met. Target projects always need to be changed, to add my .cpp file as one compiling unit. So, I publish only header files finally, they can only use #include to use my code now. yanlinlin82@gmail.com |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cisco 2620 static natting help | biomed32uk | Hardware | 1 | 10-22-2009 03:25 PM |
| please help: simple java coding error 'cannot be referenced from a static context' | clm90 | General Help Related Topics | 0 | 10-17-2009 06:49 AM |
| Passing value with out using variable in query string in PHP! | Ali_ggl | General Help Related Topics | 0 | 11-29-2008 12:22 PM |
| Shag Floor danger? | Dave Hardenbrook | A+ Certification | 3 | 02-28-2008 05:13 AM |
| Variable Scope in asp.Net | jansi_rk | Software | 1 | 09-18-2006 06:05 PM |