does this make sense ??

This is a discussion on does this make sense ?? within the C++ Programming forums, part of the General Programming Boards category; does this given class make sense, if yes where can it be useful ?? Code: class A{ public: int a[]; ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33

    does this make sense ??

    does this given class make sense, if yes where can it be useful ??
    Code:
    class A{
      public:
       int a[];
    };

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    if someone define a class like this , Why doesn't he just use a namespace ?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    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?

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    The road is even, but human choose path.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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?
    You'll need to use dynamic memory allocation, or an STL container or something that does the allocating for you, such as vector.

    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Does your class compile? If it doesn't compile, I would not consider it very useful.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    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;
    }

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Quote Originally Posted by agarwaga
    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 helps
    Amish

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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?

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Quote Originally Posted by 7stud
    Code:
    !#$#@$@ forum software.
    
    ...and gg [ 2 ] would be what?
    When I run his code,
    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

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I am curious why his compiler would allow a[].
    a[] is probably the same as *a, just like in parameter lists.

    This:
    Code:
    void function(int array[]) {
    is the same as
    Code:
    void function(int *aray) {
    so it would make sense if your's was the same way.
    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.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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:
    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;
    }
    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.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by axr0284
    When I run his code,
    I get
    2
    3
    Then, why did you post the results as:
    C:\Borland\BCC55\Bin>hello
    2
    1

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does this even remotely make sense
    By hckr83 in forum C Programming
    Replies: 6
    Last Post: 12-23-2005, 04:02 PM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  4. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM
  5. Foreigners don't make sense
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-25-2002, 02:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21