• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

What does this mean?

 
Ranch Hand
Posts: 574
1
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm an expert at C and fairly good at Java.  I volunteered to help a friend with a C++ codebase I'm having trouble making heads or tails of.  Example:


tl;dr How do I interpret the ConfiguredConsumer constructor?

This is inside a .hxx file, and I'm 90% sure it's not a template (lots of macros above it I haven't quite sussed out yet).  What do I duckduckgo to find out how to understand "SomeMethod(args) : something(args?), somethingElse(more args?), yetAnotherSomething(stuff) {}"?  I've got a couple C++ books, most recent dated 2017, and I'm not seeing anything in them that helps decipher this line.

I'm at a total loss here.
 
Rancher
Posts: 4190
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bits after the ':' are the initialisers for the member data for the instance.

So I assume that there are (at least) three attributes for the class, impl_, consumer_ and cfg_.
 
Saloon Keeper
Posts: 10434
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called a "member initializer list": https://en.cppreference.com/w/cpp/language/initializer_list

It's similar to assigning initial values to those members from the body of the constructor, except that some members MUST be initialized before the constructor body runs. The impl_, consumer_ and cfg_ members are likely references or const qualified.
 
Jim Venolia
Ranch Hand
Posts: 574
1
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Belated thanks, I'm just now getting back to this code and your replies helped a lot.

One thing about this code seems very bad to me.  Several classes are defined in header (.hxx) files.  Is this normal in C++?  If so, my learning curve pretty much just went 90 degrees.

Templates, yeah I get that.  But classes?  What happens when 2 .cxx files include a .hxx defining a class?
 
Stephan van Hulst
Saloon Keeper
Posts: 10434
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not an expert in this area, but I would guess that if you have a class that has only one sensible implementation that very very rarely changes, you could put the definition in the header file to make it more portable.

The major downside is that when the header is included the entire definition is inlined, which makes compilation times much slower if the class is included in many source files. It also bloats your assemblies and may lead to the compiler complaining about multiple definitions given for the same token.

You're right, this practice seems very questionable. If you can reach the original author, you can ask them for their reasons.
 
Jim Venolia
Ranch Hand
Posts: 574
1
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out they put the classes in .hxx files to inline the code.  They're running on small embedded devices and had serious performance problems.  They traded code size for speed, and instrumented the results to prove it.  
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!