• Advertisement
Sign in to follow this  

literal strings and parameters and classes oh my!

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's set the scene. C++ I have a custom class String that is created to act like Java's String class. I have a debug function like so
void debug(String s)
{
  ..do some stuff and print the string..
}

Now I have, within another function, something like this
...
  int someVariable = 14; // random value for demonstration
  debug("literal string" + someVariable);
...

What I am wanting to happen is "literal string" constructed to a String and String's + operator to be called adding "14" to the end of it. Instead what is happening is the address to "literal string" is being incremented by 14 and String's constructor is being called with some invalid location to a char*. It makes sense in a way, to me, that what I am trying to do is invalid (it was a "hope for the best" moment) as the + operator is ambiguous. Although if it SHOULD work please let me know. What I want to know is, is there a better way to do this than something like, for example...
...
  debug(String("literal string") + someVariable);
...

Or constructing a string before hand. I'm hoping to allow the debug function to be called in one of several ways
  debug("literal string" + someVariable);
or
  debug("literal string %d",someVariable);

But I have heard that variable parameter lists in C++ should really be avoided. So what are my options?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
What I want to know is, is there a better way to do this than something like
Overload operator+ to take a const char* (or char* - can't remember) on the left and right hand sides of operator+. Look here.

Share this post


Link to post
Share on other sites
Writing something like debug("my string" + myInt) will never work, because the left argument is of type const char * and the right is of type int, and you can't change the way operators for built-in types work (when there are built-in types on both sides, that is). If you don't want to use variable argument lists (and you shouldn't), you can use something like stringstreams:


ostringstream os;
os << "myInt = " << myInt;
debug(os.str().c_str());



Maybe there's some way to make it more compact. Something like this:


MyCustomStringStream debugStream;
debugStream << "myInt = " << myInt; // This will call your debug() function



I'm not sure if the latter is possible but maybe you'll figure out a way to do it.

Hope this helps.

Share this post


Link to post
Share on other sites
If you mean something like...

String operator+ (const char* a, const char* b);

...

error C2803: 'operator +' must have at least one formal parameter of class type

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64


Thanks, this pretty much confirms what I was thinking to begin with. Looks like I need to rethink the way debug works.

Share this post


Link to post
Share on other sites
Quote:
Original post by instinKt
If you mean something like...

String operator+ (const char* a, const char* b);

...

error C2803: 'operator +' must have at least one formal parameter of class type

One side should be your string class iirc

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64

I'm not sure if the latter is possible but maybe you'll figure out a way to do it.

Hope this helps.


It is possible, just a little tricky. You would have to subclass a streambuf and override a few methods. If i remember correctly, sync, overflow and xsputn should be overridden at the very least for an output stream. The streambuf can then be plugged into an ostream object.

Share this post


Link to post
Share on other sites
Well I did pretty much what Gabe was suggesting, but I think a fair bit simpler.

I just created a DebugStream class with no inheritance, and just overloaded the << operator for different types. When an 'end character' comes through the stream the current stream is printed and possibly cleared depending on user settings.

Then I just have a global debug stream and use it like so...
__debug << "This is a debug test: " << someInt << DebugStream::END;

Might not be as flexible as it could be if i subclassed from the stringstream class, but this does exactly what I need it to.

Thanks for the help guys.

Share this post


Link to post
Share on other sites
Because it's a development for an embedded system and a lot of the core functionality is handled with custom interfaces etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement