Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What does this mean?

 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15656
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15656
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  
 
What's a year in metric? Do you know this metric stuff tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic