Shared variable and function identifier/name problem

Hello,
I'm facing some issue with de following idea :
I try to use the same name for a variable and some functions... but indeed there is no way to compile it without errors.

Here is a very simplified example presented with my class PrintStream derived from ostream :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

namespace hbt
{
	// "pout" return a singleton instance of the PrintStream class inheriting ostream.
	PrintStream &pout = PrintStream::getInstance();

	// "pout(  )" return the same singleton instance as "pout" but call the verbosity() function before returning it.
	PrintStream &pout( const int verbo )
	{
		PrintStream::getInstance().verbosity( verbo );
		return pout;
	}

	// "pout(  )" return the same singleton instance as "pout" but call the verbosity() function and the setChannels() function before returning it
	PrintStream &pout( const int verbo, const vector<int> chans )
	{
		PrintStream::getInstance().setChannels( chans );
		PrintStream::getInstance().verbosity( verbo );
		return pout;
	}

	// etc... others pout() functions
}


and here is an example of how I would like to use it :

1
2
3
4
5
6
7
8
9
10
11
12

int main(int argc, char *argv[])
{
	// initiate the PrintStream class singleton instance AND push "Test01" in the stream.
	hbt::pout << "Test01" << std::endl;

	// push "Test02" in the stream AND call the verbosity function with the value "2".
	hbt::pout(2) << "Test02" << std::endl;

	// push "Test03" the my stream AND call the verbosity function with the value "2" and the channels function with the value 1,2,3 (C++0x required to initiate the vector this way).
	hbt::pout(2, {1,2,3}) << "Test03" << std::endl;
}


Actually, all this stuff works very well as long as I do not use the same name for the pout reference and the pout() functions (I currently use poutv( const int ), poutv( const int, std::vector<int> ), etc... instead of pout()).

But using the same name for the reference and the functions would be cleaner than using different names, so is there any way to make the functions use the same "identifier" as the variable without overriding error ?

I'm almost beginner at C++ so do not hesitate to point out all my errors...

Thank you
Last edited on
You could Turn this one:
1
2
    // "pout" return a singleton instance of the PrintStream class inheriting ostream.
    PrintStream &pout = PrintStream::getInstance();


into a function that takes no arguments:
1
2
3
4
5
// "pout" return a singleton instance of the PrintStream class inheriting ostream.
    PrintStream &pout() 
{
      return  PrintStream::getInstance();
}


That should work
Last edited on
@guestgulkan:
You are right, it does work.

But "pout << ..." is cleaner than "pout() << ... " hehe.

And more important, I'm curious to know is there is a way to use the same identifier for a function and a variable using any special way to help the compiler to make the difference between them just as with two functions using the same name but different arguments.
Last edited on
Not in the same scope/namespace like you are attempting that I can think ok anyway.
Could you overload operator() in class PrintStream?
@ne555 :

That's it ! It works like a charm...

here is what i added to the PrintStream class :

1
2
3
4
5
PrintStream &operator()();
PrintStream &operator()( const uint verbo );
PrintStream &operator()( const uint verbo, const uint channel );
PrintStream &operator()( const uint verbo, const std::vector<int> channels );
PrintStream &operator()( const std::vector<int> channels );


and now i can use my "pout" stream with or without arguments and with or without parenthesis.

@guestgulkan : Yes i think you are right, I can't see anyway to share the same identifier between functions and variables, directly in the same namespace.


So finally, you both helped me to achieve what i was trying to do, finding a way to call pout with and without parenthesis, and learn if it is possible or NOT, to give the same identifier to a variable and a functions.

Thank you very much for you help.
Naa
Topic archived. No new replies allowed.