0

Here is a simple "hello world" program.

int main(){
  std::cout << "hello world\n";
  return 0;
}

How do I make the program print "hello world"+{some other string} for example: "hello world this is my string" without touching and changing the main ?. I tried to overload the << operator of string but it didn't work. thank you.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Arikost
  • 9
  • 1
  • 3
    *How* did you attempt to overload the `<<` operator? – Some programmer dude Jun 28 '21 at 16:12
  • Also, whay do you want to do that overloading to add extra information? If it's just plain curiosity then that's okay, but please state so. Otherwise please [edit] your question to ask about the *actual* problem you want to solve, telling us what you think might solve it. Always ask for the actual problem, to avoid your question being [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Jun 28 '21 at 16:21
  • Do you want this overload only for `"hello world\n"` or any character literal string? – Thomas Matthews Jun 28 '21 at 17:46

3 Answers3

3

You can do this, but you really shouldn't.

#include <iostream>

std::ostream& operator<<(std::ostream & os, const char (&)[13]) {
    return os << "hello world this is my string\n";
}

int main(){
  std::cout << "hello world\n";
  return 0;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • This is the solution I was looking for .. so thank you very much I just did not define the char* parameter as const... my bad – Arikost Jun 28 '21 at 17:01
  • I didn't think this is allowed by the standard. If I'm wrong please let me know. – Bathsheba Jun 28 '21 at 17:02
  • @Bathsheba it's not adding to `std`, so I don't know why it would be prohibited. Interestingly `const char *` in `::` is unambiguously worse than the one in `std`, and `const char (&)[N]` is ambiguous – Caleth Jun 28 '21 at 17:44
2

How do I make the program print "hello world this is my string" without touching and changing the main ?

You are asking for sneaky tricks, I presume? Macros are infamous for being sneaky.

int main(){
  std::cout << "hello world this is my string\n";
  return 0;
}
#define main not_main

// "the main", below, has not been touched.

int main(){
  std::cout << "hello world\n";
  return 0;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

It is possible to do this in a defined manner. You've made it difficult due to the newline character which we somehow need to workaround. (We can't rely on a destructor for example). What we need to do is to make our own output stream class, and set it to be the one that std::cout uses:

  1. Derive a class from std::ostream.
  2. Implement operator<< for const char*. That needs to replace \n with the extra text you want, or something along those lines.
  3. Imbue that stream into std::cout using rdbuf: https://en.cppreference.com/w/cpp/io/basic_ios/rdbuf

Clearly all this fun can take place outside main. (3) needs some thought: you can use a constructor and instantiate a static instance of that class above main. That's an idiomatic way of running code before main.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483