May I ask you a bit stupid question?

Jun 8, 2013 at 1:02pm
Hello.

Recently, I was looking through the source code of the app written in India(Bangalore) and I found those lines of code:

1
2
private:
   #define TIME_OUT   20000 


I have the only question: What a hell? How does the author of those lines of the code written above intend to do the preprocessor "private"? O_o Am I missing something?

p.s. it's C++. Or it looks like...........................

Thank you beforehands for your answers
Last edited on Jun 8, 2013 at 1:37pm
Jun 8, 2013 at 1:12pm
There is no any relation between line private: and the macro definition. The only meaning that the macro will be known for the preprocessor after the line with private:.
IMO it is a very bad style of programming,
Jun 8, 2013 at 1:13pm
private:
#define TIME_OUT 20000

when the preprocessor run, it will replace TIME_OUT with 20000. it does not matter where you define it. Here may be developer was thinking that TIME_OUT will not be visible outside the class, but this is not correct. We can access TIME_OUT out side the class
class A
{
private :
#define TIMEOUT 20000
};

int main(int argc, char *argv[])
{
cout<<" TIMEOUT "<<TIMEOUT<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

output is 20000
Jun 8, 2013 at 1:22pm
Thank you! I've already begun to think that may be I don't know something about Cpp =)

Thank you once again!

p.s. And this private actually was destined for the bunch of #defines. You know, I didn't expect to see anything like this...
Last edited on Jun 8, 2013 at 1:26pm
Jun 8, 2013 at 1:39pm
> You know, I didn't expect to see anything like this...

You see this construct a lot in open source code. KDE, Android and the like.
For instance: http://android.googlesource.com/platform/hardware/qcom/media/+/android-sdk-support_r11/mm-video/vidc/vdec/inc/ts_parser.h

I guess the intent is to document that the #define is intended to be used only by the implementation.
Though why it shouldn't be
1
2
3
4
5
6
7
8
9
// ...
private:
	// #define TIME_SZ 64
        static constexpr std::size_t TIME_SZ = 64 ;
        // or in C++98: enum { TIME_SZ = 64 } ; 
        // ...
	typedef struct time_stamp_list {
		timestamp input_timestamps[TIME_SZ]; 
   // ... 

is beyond me.
Jun 8, 2013 at 3:17pm
yeah, it seems, I have to read more carefully an open source code... and don't ask stupid questions on forums. Thank you for your answer!
Jun 9, 2013 at 11:12am
Apparently not just open source code...

From xstring provided with Visual C++ 2008

(_STR_ITER_BASE appears to be used just once, so I don't see the point of it.)

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
		// TEMPLATE CLASS basic_string
template<class _Elem,
	class _Traits,
	class _Ax>
	class basic_string
		: public _String_val<_Elem, _Ax>
	{	// null-terminated transparent array of elements
public:
	typedef basic_string<_Elem, _Traits, _Ax> _Myt;
	typedef _String_val<_Elem, _Ax> _Mybase;
	typedef typename _Mybase::_Alty _Alloc;
	typedef typename _Alloc::size_type size_type;
	typedef typename _Alloc::difference_type _Dift;
	typedef _Dift difference_type;
	typedef typename _Alloc::pointer _Tptr;
	typedef typename _Alloc::const_pointer _Ctptr;
	typedef _Tptr pointer;
	typedef _Ctptr const_pointer;
	typedef typename _Alloc::reference _Reft;
	typedef _Reft reference;
	typedef typename _Alloc::const_reference const_reference;
	typedef typename _Alloc::value_type value_type;

#define _STR_ITER_BASE(it)	(it)._Myptr

	typedef _String_iterator<_Elem, _Traits, _Alloc> iterator;
	typedef _String_const_iterator<_Elem, _Traits, _Alloc> const_iterator;

	// snip 


And the single use

1
2
3
4
5
	static size_type __CLRCALL_OR_CDECL _Pdif(const_iterator _P2,
		const_iterator _P1)
		{	// compute safe iterator difference
		return (_STR_ITER_BASE(_P2) == 0 ? 0 : _P2 - _P1);
		}


Last edited on Jun 9, 2013 at 11:24am
Topic archived. No new replies allowed.