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, ...)
and
fn( ..., 0, ...)
But now we have :-
class B : public A {...}
and fn(..., 0, ...) is declaring that parameter 2 is now ambiguous.
So what I wanted was to declare a 'NullA'.
But it has to be declared in a header and there are multiple C++ files using
the header and they all get linked to gether. So declaring in the header :-
const NullA* = 0;
is out.
The only way I have found is :-
class A {
public:
const Null;
};
then is one C++ file :-
const A::Null = 0;
and
fn( ..., A::Null, ...)
But the 'A' name is rather long and I would have prfered an out of class
solution with A's acronym.
I am probably forgetting something very basic about C++.
Any help welcome,
Aaron
Should be :-
class A {
public:
static const Null;
"Aaron Gray" <ang.u...@gmail.com> wrote in message
news:78f7klF...@mid.individual.net...
>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, ...)
>
> and
>
> fn( ..., 0, ...)
>
>
> But now we have :-
>
> class B : public A {...}
>
> and fn(..., 0, ...) is declaring that parameter 2 is now ambiguous.
>
> So what I wanted was to declare a 'NullA'.
>
> But it has to be declared in a header and there are multiple C++ files
> using the header and they all get linked to gether. So declaring in the
> header :-
>
> const NullA* = 0;
>
> is out.
>
> The only way I have found is :-
>
> class A {
> public:
> const Null;
> };
class A {
public:
static A* Null;
};
> then is one C++ file :-
>
> const A::Null = 0;
A* A::Null = 0;
This works I would like it to be a const but the function it is being passed
to does not take a const A*, and that would require casting.
The other alternative I came up with is :
#define NullA ((A*) 0)
Oh forgot about extern, really not with it today !
But the const'ness is still a problem. So may use preprocessor.
Aaron
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 using
> 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