Re: const across multiplely linked files
On 31 Mai, 13:23, "Aaron Gray" <ang.use...@gmail.com> wrote:
I am trying to declare a const in a header file but have forgetten how.
I have a class and I want a Null value for that struct to pass as an
argument when it is not being used. Now it used to be done passing a '0',
but the class now has an iheriting child class e.g. :-
class A {...}
fn( ..., A a, ...)
What's that supposed to be? A function declaration?
and
fn( ..., 0, ...)
And that? A function call expression? Or is this supposed to be an
overload?
But now we have :-
class B : public A {...}
and fn(..., 0, ...) is declaring that parameter 2 is now ambiguous.
This all seems /very/ strange in terms of design. Why would you want
to pass an object that's part of an inheritance hierachy by value? Are
you sure that you don't misuse public inheritance and/or overloading
and/or pass-by-value?
So what I wanted was to declare a 'NullA'.
But it has to be declared in a header and there are multiple C++ files us=
ing
the header and they all get linked to gether. So declaring in the header =
:-
const NullA* = 0;
is out.
Did you mean something like this?
A* const NullA = 0;
There's no problem with putting this definition inside a header that's
included in many files. This is a pointer constant and has internal
linkage.
The only way I have found is :-
class A {
public:
const Null;
};
then is one C++ file :-
const A::Null = 0;
There's a lot wrong this this piece of code. C++ is statically typed
language. It doesn't let you declare variables without a type. Also,
you forgot to add "static" as a storage qualifier.
[...]
I am probably forgetting something very basic about C++.
Yes. You should brush up your C++ knowledge. C++ is not Java where a
declaration
void foo(A a) {...}
passes a reference to an object. There is no implied indirection in C+
+. If you want to pass objects by reference or by address you need to
declare it (&, *). Also, post more details about what you're trying to
do (not just how) including a bit of code without any "..."s.
Cheers!
SG