1.Can a 64-bit OS tell whether a given EXE file is a 32-bit program or a 64-bit program and run it correctly?
2.Can a 32-bit OS run a 64-bit program if the processor supports 64-bit programs?
3.Do I specify default values for template parameters only in the general class definition or it's legal/relevant (or has a special use) to specify them in specifications?
4.Which is better/correct for a member function, MyClass& func() const; or const-ness overloading using two functions MyClass& func(); and const MyClass& func() const;? And what if the return value is a value instead of a reference (MyClass instead of MyClass&) ?
3. If your specialization does not specialize all default parameters, then it is probably best to respecify the same default values. I did not try either way to see if one compiled and one didn't though.
4. It depends on what func() does. Presumably, since func() is returning a reference to something, that something is probably a data member of the containing object. That being the case, would you want the user to be able to call non-const methods on MyClass, thus changing the contents of MyClass and effectively the containing object as well? The general answer is no, but as for any rule, there are always exceptions. On the other hand when returning by value, you are returning a copy. At that point whether you return a const copy or a non-const copy is essentially irrelevant.
EDIT: And the answer to #1 has to be yes. Obviously it is possible to run 32-bit apps on 64-bit processors, just the same as in the days of Windows 95 when you could run "older" 16-bit apps in a (largely) 32-bit OS.
(("what would be the point in a 64-bit OS?" - It can be twice as fast as an equivalent 32-bit OS...))
Thanks for the answers!
I have 3 new questions:
1 2
constint& ref();
constint* ptr();
These functions return a private object, which cannot be accessed directly. But I'd like functions to only have the ability to get the value, not change the object. If these functions were global, I wouldn't need them, because I can simply return the object by value. But what if they are const member functions? This possibility exists:
1 2
int& f();
int f() const;
1. Is the return type part of the signature, at least in this case? In other words, will this work like const-ness oveloading? Will the first function run for non-const objects the the second for const objects?
I won't be surprised if the answer to no. 1 is no, because the following method is sometimes used (actually, I never saw code that uses the first method(the above one)):
1 2
int& f();
constint& f() const;
or this:
1 2
int* f();
constint* f() const;
I know const-ness overloading will work here and that const pointers and references won't allow changing the object.
Can the object still be changed by getting its address? I'm guessing some low-level methods can write to memory belonging to a const object, but I'm only asking about copying the address to a non-const pointer. For example, there's a class called MyClass. It has an int data member called memb and two member functions: ref() returns a const reference to memb and ptr() returns a const pointer to memb. 2. Is the following code legal:
1. extern "C" tells the compiler that the functions declared inside are defined in C, so the declarations shouldn't be mangled. extern tells it the symbol it applies to is defined in a different compilation unit.
As a C++ programmer, do I need to use extern "C" when dealing with C source code written for libraries by other people? Or extern "C" is of no interest to me (as I program in C++, not C)?
If I use header files like OpenGL's or Linux sys headers, that already have extern "C" inside them, Can I simply include them with #include<GL/gl.h> , etc.? (since the extern "C" is already in the header file itself)
extern means this variable is only declared here, it's defined elsewhere. The linker is expected to tie things together.
extern"C" defines C linkage.
Although C and C++ functions look the same, they are given different names internally. In C, the name is largely unchanged. In C++ the names are mangled with the signature to support typesafe linkage.
In C, the linker only matches on function name, in C++ the linker matches on the name, parameters and return types to catch silly programmer mistakes.
If a C module is to be linked with a C++ module, the C++ naming must be relaxed. extern"C" does the relax.
The problem is that C and C++ programs use the same extensions. In the beginning, C used .h/.c and C++ used .H/.C; but when it moved off UNIX onto DOS, new conventions were required as FAT is case-insensitive. Moves to .hpp/.cpp and hxx/.cxx happened, but we seem to have settled with .h/.cpp. This means you cannot determine if a source file is C or C++ without peering into it, which means that header files that support both languages need the pattern helios showed.