Few Questions

I could get the answers by trying things with a compiler, but I'm very very very busy, so I'm asking here. It saves time, of which I hardly have any for programming. ( please ignore the weird indentation...it's not intended...)

1. Inside a header file, there is #define NONE 0 . The header file is included in a source file. Will the #define replace all the NONE in the source file?

2. Can macro definitions affect other macro definitions?
1
2
#define ABC 0
   #define ABCD 2 

Will the first #define convert the second one into #define 0D 2 ?

3.
1
2
#define A 0
    #define AB 1 

Does this guarantee that every A is replaced by 0 before any AB is replaced by 1 (supposing the answer to question no. 2 is no) ?

4. Does typedef work with classes?
1
2
3
4
5
class MyClass
   {
     /* . . . */ 
   };
   typedef MyClass* MyPtr;


5. How do I use a global variable outside the source file in which it is declared? (let's say file1.cpp contains int a=0; in the global scope. How do I use the variable a in file2.cpp ?

6. Can class A have a static member of type A?

7. Can new allocate arrays of pointers?
1
2
int** ptr = new int* [5];
   MyClass** ptr2 = new MyClass* [5];


8. What is the common value (or what are the common values) of CLOCKS_PER_SEC
(which is defined in the ctime library) ?

9. Does converting a variable of type double containing a positive value smaller than 2^32 (^ is power here...) into unsigned int simply cut the decimal part?

10. Can static members be private? Can a private static data member be initialized in the global scope (according to the tutorial, static members are initialized in the global scope, but generally private members can't be accessed from the global scope, so I'm wondering...) ? If not, where is it initialized?

11. Do I have to specify the scope when using a function that is define inside a namespace? For example, if I use the function clock() without using namespace std; , do I have to type std::clock(); when I use the function?

12. Inside member functions, members can be written without the this pointer (member instead of this->member ). Does it work with member functions too?

13. Is there a difference (in ASM and machine code) between these:
1
2
int a=1;
    func(a);


func(1);

If there's a difference, which func() is faster, func(a) of func (1) ?


Help will be highly appreciated...I really need the answers.
:)
Last edited on
1. Yes.
2. Not the way you indicate. A macro will never replace a part of a token, only the whole token.
3. Again, a macro only matches the whole token. So A will only replace lone A's while AB will replace lone AB's.
4. Yes.
5. Put extern int a; in the other file.

and I'm done.
6. Yes, to my own surprise.
7. Yes.
8. It could be anything, but the most common are multiples of 1000. For example, in Windows it's 1000, and in Linux it's 1000000.
9. Converting from any floating point value to an integral always truncates the fraction.
10. Both tatic private members and definitions of static private members at global scope are legal.
11. clock(), just like the rest of the functions in the C library, is not in the std namespace. I'll assume you meant std::min(). Yes, yes you have. IMO, it's better practice to never use using.
12. Yes. By the way, member functions are called "methods", and member variables/objects are called "properties". "Member" can mean either of the two. Also, the implicitness of this-> is just syntactic sugar. In reality, you're always dereferencing this even if you don't make it explicit.
13. Depends on compiler optimizations. Without any optimizations and without inlining, they're almost equivalent. In the former, the variable will be pushed onto the stack next to other stuff, and the function will be called. There may be a little overhead if the CPU didn't have the variable cached at that moment. In the latter, the value to be pushed was obtained with the instruction, so there won't be any loading, and the value can be pushed immediately.
Unless you're writing something that needs to run at ludicrous speed, you should never consider things so low-level while coding. There's a lot of high-level optimizations you can perform before you get to this.
All questions have been answered - Thanks you!!! - but I have three more:

1. #define A B #define A C
Now these are whole tokens...Will executing the first line convert the second line into
#define B C ?

2. So even private members can be initialized in the global scope...but are they REALLY private? Can they be accessed from anywhere like global variables (adding scope specification befoe the name, of course) or only from within the class like any normal (non-static) private member?

3. Is the ctime library a C++ version of time.h ? I thought all the C++ standard libraries are in the std namespace? How can I tell which are in std and which aren't? (Hmmm...maybe the ones that don't exist in C are...)


And about question no. 13 . . . I'm not doing such optimizations, don't worry. I'm just considering replacing my macro definitions with constant variables. And I was wondering whether things will work more slowly this way...
Last edited on
2. Yes, they're private. The fact that they're defined at global scope is irrelevant.
3. It's the C++ version of the C library, but notice that it's just a header that includes the other and disables the deprecation warning (or least that's the case in VC++). Only the C++ standard library is in the std namespace. The standard library inherited from C remains in global scope. It's easy to tell which is which because all C functions are six characters or less, and everything in the CPPSL starts with std::, but you have to avoid using to see it, which is something I always encourage.
Topic archived. No new replies allowed.