Struct within a class

D

dust

Hi,
I have the following senario:

foo.h:

class A
{
...
struct B //public here
{
...
};
void func (const B& var1, int var2);
....
}

Now I have to test the function func in another module (test.cpp
file). I have #included this header file and created a object 'obj' of
type class A.
How can I create an object of type struct B so that I can pass this
object as parameter to function 'func()' which I'm testing in
'test.cpp'
 
V

Vladimir Jovic

dust said:
Hi,
I have the following senario:

foo.h:

class A
{
...
struct B //public here
{
...
};
void func (const B& var1, int var2);
....
}

Now I have to test the function func in another module (test.cpp
file). I have #included this header file and created a object 'obj' of
type class A.
How can I create an object of type struct B so that I can pass this
object as parameter to function 'func()' which I'm testing in
'test.cpp'

A::B obj;
 
V

Victor Bazarov

Isn't this the syntax if the struct is static only??

It *is* static, sort of. There is only one of 'B' in 'A' (IOW, there is
no separate 'B' in every instance of 'A'). If it's easier for you,
think of a nested type definition as "an implicitly static member".

V
 
S

Stefan van Kessel

Isn't this the syntax if the struct is static only??

I believe you are confusing that with static members. A nested struct is
not itself associated with particular instances of the enclosing class.
Only instances of the struct can be static / non static. According to
7.1.1 declaring a struct static isn't even valid C++ (MSVC accepts it
nonetheless):

"In C++, the static or extern specifiers can only be applied to names of
objects or functions Using these specifiers with type declarations is
illegal in C + +. In C, these specifiers are ignored when used on type
declarations. Example:
static struct S { // valid C, invalid in C + +
int i;
// ...
};
Rationale: Storage class specifiers don’t have any meaning when
associated with a type. In C++, class members can be defined with the
static storage class specifier. Allowing storage class specifiers on
type declarations could render the code confusing for users."
 
J

Juha Nieminen

dust said:
Isn't this the syntax if the struct is static only??

'B' is a type name. Type names cannot be "static". The concept of "static"
does not make any sense with type names in C++. Only member *objects*
(including member variables) and member *functions* can be static (in which
case they are usually called class variables and class functions).

There may be other object-oriented languages where types are objects,
and there it might make sense for a type to be "static", but C++ is not
such a language.

The :: operator is used to refer to nested types, be it inside namespaces
or classes/structs.
 
J

James Kanze

It *is* static, sort of. There is only one of 'B' in 'A' (IOW, there is
no separate 'B' in every instance of 'A'). If it's easier for you,
think of a nested type definition as "an implicitly static member".

Calling it static still confuses the issue, I think. Types
(classes, enums) always have global binding, and aren't runtime
objects, so don't have lifetime. Static is overloaded in many
ways, but they all involve either name binding or object
lifetime. Since you can't give a class internal binding (one
meaning of static), and it doesn't have a lifetime (the other
meaning), the word has no meaning for classes.
 
J

James Kanze

'B' is a type name. Type names cannot be "static". The concept
of "static" does not make any sense with type names in C++.
Only member *objects* (including member variables) and member
*functions* can be static (in which case they are usually
called class variables and class functions).
There may be other object-oriented languages where types are
objects, and there it might make sense for a type to be
"static", but C++ is not such a language.

I think he's confusing C++ and Java. In Java, a "nested class"
can only be created in a non-static member function of the
enclosing class, contains an implicit pointer to the enclosing
class, and can access all of the members of the enclosing class.
Java overloads the keyword static to turn this "feature" off.
 
V

Victor Bazarov

Calling it static still confuses the issue, I think. Types
(classes, enums) always have global binding, and aren't runtime
objects, so don't have lifetime. Static is overloaded in many
ways, but they all involve either name binding or object
lifetime. Since you can't give a class internal binding (one
meaning of static), and it doesn't have a lifetime (the other
meaning), the word has no meaning for classes.

You're being too strict, I think. The meaning of static for members of
a class is that there is only one of it for all instances of the class
("shared" member). That's what I meant.

V
 
D

Daniel Pitts

Calling it static still confuses the issue, I think. Types
(classes, enums) always have global binding, and aren't runtime
objects, so don't have lifetime. Static is overloaded in many
ways, but they all involve either name binding or object
lifetime. Since you can't give a class internal binding (one
meaning of static), and it doesn't have a lifetime (the other
meaning), the word has no meaning for classes.
It could be that dust has experience in Java, where nested classes can
be declared as static, or not. If they are not, they receive a hidden
pointer to the "this" of the enclosing classes instance. I'm assuming
C++ is different in this regard, and that any struct inside of a class
is a regular struct with a different "name space" (not to be confused
with namespace.)
 
A

Andrey Tarasevich

Isn't this the syntax if the struct is static only??

Struct (as in your example) cannot be "static". Only _data_ _members_
and _member_ _functions_ can be static or non-static. _Types_ (like your
struct) cannot be static or non-static. The concept of being "static" is
simply not applicable to types.

The `::` syntax is a scope resolution operator. It has nothing to do
with something being static or not. The `A::` prefix simply indicates
that you are referring to something declared in the scope of class `A`.

Of course, one should note that in order to use `A::B` as shown above,
that `A::B` shall be accessible fro the context where you declare your
`obj`.
 
D

dust

It could be that dust has experience in Java, where nested classes can
be declared as static, or not.  If they are not, they receive a hidden
pointer to the "this" of the enclosing classes instance.  I'm assuming
C++ is different in this regard, and that any struct inside of a class
is a regular struct with a different "name space" (not to be confused
with namespace.)

I'm experienced in neither c++ nor java, but in C. I'm new to C++ and
OOP.
Its also true that I should have spent a few minutes thinking about it
before
replying to Vladimir :)

Thanks you all for your comments.
 
J

James Kanze

On 9/21/2010 1:19 PM, James Kanze wrote:

[...]
You're being too strict, I think. The meaning of static for members of
a class is that there is only one of it for all instances of the class
("shared" member). That's what I meant.

I think you're right:). Formally, there is an effect on
lifetime---a static member has static lifetime, whereas a non
static member has the same lifetime as the instance which
contains it. But for the user, the important issue is that the
static member is shared between all instances, where as each
instance has its own instance of a non-static member.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top