does this given class make sense, if yes where can it be useful ??
Code:class A{ public: int a[]; };
does this given class make sense, if yes where can it be useful ??
Code:class A{ public: int a[]; };
if someone define a class like this , Why doesn't he just use a namespace ?
My problem is with the array's length not being specified. Can it make sense in some case ?
Btw, how do you give the size of this array afterwards?
The road is even, but human choose path.
You'll need to use dynamic memory allocation, or an STL container or something that does the allocating for you, such as vector.My problem is with the array's length not being specified. Can it make sense in some case ?
Btw, how do you give the size of this array afterwards?
ohheart, as to your first post, I think this class was just an example, and your second post doesn't make sense.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Maybe you're thinking of this:
http://gcc.gnu.org/onlinedocs/gcc-4....ml#Zero-Length
Does your class compile? If it doesn't compile, I would not consider it very useful.
it does compile (Borland C++ 5.5.1 for Win32) , leave the class mentioned above
even the code mentioned below compiles , although it doesn't produce the result i expected
C:\Borland\BCC55\Bin>bcc32 -I"c:\borland\bcc55\include" -L"c:\borland\bcc55\lib"
H:\winnt\Desktop\hello.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
H:\winnt\Desktop\hello.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
C:\Borland\BCC55\Bin>hello
2
1
Code:#include <iostream> #include <fstream> using namespace std; struct B{ public: int gg[]; }; class A{ public: int a[]; int b; B bb; } a = {{1,2},3,{{1,2,3}}}; int main () { cout<<a.a[1]<<"\n"; cout<<a.bb.gg[2]; return 0; }
it would seem the line a = {{1,2},3,{{1,2,3}}}; is assigning a[]={1,2}, b=3 and bb={1,2,3} which efectively puts gg={1,2,3} in the stucture B called bb. Class A would then work like a structure called 'a'. then the output makes sense. It beats me why this would be helpful though. Hope it helpsOriginally Posted by agarwaga
Amish
which efectively puts gg={1,2,3} in the stucture B called bb...then the output makes sense.Code:!#$#@$@ forum software. ...and gg [ 2 ] would be what?
When I run his code,Originally Posted by 7stud
I get
2
3
which matches my answer although ms vs 2005 does not allow a[]. so i had to make it a[2].
I am curious why his compiler would allow a[]. Maybe it's a bug.
Amish
a[] is probably the same as *a, just like in parameter lists.I am curious why his compiler would allow a[].
This:
is the same asCode:void function(int array[]) {
so it would make sense if your's was the same way.Code:void function(int *aray) {
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>a[] is probably the same as *a, just like in parameter lists.
And if that is the case, agarwaga is invoking undefined behavior in the code snippet, as there's no memory allocated. The code should really look like:
And if you really need dynamically allocated memory, use a vector<int> instead of an int array, and use push_back() to add elements to the vector.Code:#include <iostream> #include <fstream> using namespace std; struct B{ public: int gg[3]; }; class A{ public: int a[2]; int b; B bb; } a = {{1,2},3,{{1,2,3}}}; int main () { cout<<a.a[1]<<"\n"; cout<<a.bb.gg[2]; return 0; }
Then, why did you post the results as:Originally Posted by axr0284
C:\Borland\BCC55\Bin>hello
2
1
>Then, why did you post the results as:
They're two different members, though both have the same first letter.
By the way if I remember correctly when I ran it I got:
2
1