Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Struct within a class

Reply
Thread Tools

Struct within a class

 
 
dust
Guest
Posts: n/a
 
      09-21-2010
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'

--
Dust
 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      09-21-2010
dust wrote:
> 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;
 
Reply With Quote
 
 
 
 
dust
Guest
Posts: n/a
 
      09-21-2010
On Sep 21, 4:24*pm, Vladimir Jovic <vladasp...@gmail.com> wrote:
> dust wrote:
> > 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;


Isn't this the syntax if the struct is static only??
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-21-2010
On 9/21/2010 7:54 AM, dust wrote:
> On Sep 21, 4:24 pm, Vladimir Jovic<vladasp...@gmail.com> wrote:
>> dust wrote:
>>> 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;

>
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Stefan van Kessel
Guest
Posts: n/a
 
      09-21-2010
On 9/21/2010 1:54 PM, dust wrote:
> On Sep 21, 4:24 pm, Vladimir Jovic<vladasp...@gmail.com> wrote:
>> dust wrote:
>>> 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;

>
> 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."
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-21-2010
dust <> wrote:
>> A::B obj;

>
> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-21-2010
On Sep 21, 1:06 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 9/21/2010 7:54 AM, dust wrote:
> > On Sep 21, 4:24 pm, Vladimir Jovic<vladasp...@gmail.com> wrote:
> >> dust wrote:


> >>> 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;


> > 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".


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.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-21-2010
On Sep 21, 4:16 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> dust <clcf...@gmail.com> wrote:
> >> A::B obj;


> > 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.


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.

--
James Kanze
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-21-2010
On 9/21/2010 1:19 PM, James Kanze wrote:
> On Sep 21, 1:06 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>> On 9/21/2010 7:54 AM, dust wrote:
>>> On Sep 21, 4:24 pm, Vladimir Jovic<vladasp...@gmail.com> wrote:
>>>> dust wrote:

>
>>>>> 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;

>
>>> 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".

>
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-21-2010
On 9/21/2010 10:19 AM, James Kanze wrote:
> On Sep 21, 1:06 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>> On 9/21/2010 7:54 AM, dust wrote:
>>> On Sep 21, 4:24 pm, Vladimir Jovic<vladasp...@gmail.com> wrote:
>>>> A::B obj;

>
>>> 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".

>
> 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.)

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
How to define an array of struct nested within a struct? Daniel Rudy C Programming 7 03-31-2006 06:54 PM
Tail space in struct within struct Michael B Allen C Programming 3 10-23-2004 08:11 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57