CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    [RESOLVED] Pointer to an array of pointers to a structure

    Hi,
    I have an abstract class like this:
    Code:
    class Obj
    {
        public:
           virtual int someFunc()=0;
        protected:
            struct A
            {
                int Index;
                std::string Name;
                std::string Hier;
            };
            A **a;
    };
    And then I derive a class from this like this:
    Code:
    class derObj : public Obj
    {
        public:
            int someFunc();
        private:
            struct A a[2] = {
              { 1, "name","Top" }, { NULL, NULL, NULL}
            }
    };
    This doesn't work out however and I get error messages:

    Code:
    error: a brace-enclosed initializer is not allowed here before '{' token
    error: ISO C++ forbids initialization of member 'a'
    error: making 'a' static|
    error: invalid in-class initialization of static data member of non-integral type 'Obj::A [2]'
    Please can anybody help me determine what is causing this error. Any help would be appreciated. Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Pointer to an array of pointers to a structure

    you cant initialise in a class def like that - use a constructor.

    you're also calling someting in derObj 'a' - the same name as A** member in Obj. Did you mean to do this?

  3. #3
    Join Date
    Mar 2011
    Posts
    4

    Re: Pointer to an array of pointers to a structure

    Thanks for the reply. I need a const array that needs to be initialized in the derived class but it needs to be accessible from the function defined in the parent class. So how can I do this?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pointer to an array of pointers to a structure

    hmm... I don't know if this is a good or bad idea, but something like this comes to mind:
    Code:
    #include <string>
    
    class Obj
    {
    public:
        virtual ~Obj() {}
        virtual int someFunc() = 0;
    protected:
        struct A
        {
            int Index;
            std::string Name;
            std::string Hier;
        };
    private:
        virtual const A& getA(int i) const = 0;
    };
    
    class DerObj : public Obj
    {
    public:
        virtual int someFunc()
        {
            return 0;
        }
    private:
        const static Obj::A a[2];
    
        virtual const A& getA(int i) const
        {
            return a[i];
        }
    };
    
    const Obj::A DerObj::a[2] = {
        {1, "name", "Top"}, {0, std::string(), std::string()}
    };
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: Pointer to an array of pointers to a structure

    seems a very bad idea unless it's going to be a singleton, surely?

  6. #6
    Join Date
    Mar 2011
    Posts
    4

    Re: Pointer to an array of pointers to a structure

    I tried this out:

    Code:
    class Obj
    {
    protected:
      struct A
      {
        int Index;
        std::string Name;
        std::string Hier;
      };
    
      explicit Obj(const A a[]) : a_(a) {}
    
      const A *a_;
    };
    
    class derObj : public Obj
    {
      static const A a_init_[];
    
    public:
      derObj() : Obj(a_init_)
      {
      }
    };
    
    const Obj::A derObj::a_init_[] = {
      { 1, "name","Top"  },
      { 0, NULL, NULL }
    };
    It compiled fine but the program crashes when I execute it. It works fine if I remove this line:

    Code:
    const Obj::A derObj::a_init_[] = {
      { 1, "name","Top"  },
      { 0, NULL, NULL }
    };

  7. #7
    Join Date
    Mar 2011
    Posts
    4

    Re: Pointer to an array of pointers to a structure

    The above scheme worked nicely. The problem was initializing the string variables with NULL. I initialized them with empty strings "" and then it worked fine.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pointer to an array of pointers to a structure

    Quote Originally Posted by aryajur View Post
    The above scheme worked nicely. The problem was initializing the string variables with NULL. I initialized them with empty strings "" and then it worked fine.
    To explain your problem, assigning NULL to a std::string produces undefined behaviour. The string constructor does not take NULL's.

    You must construct a string correctly, and assigning NULL to a string is incorrect. The reason is that NULL will be converted to a 0, and then internally the string class will call the constructor, but which constructor? No constructor of std::string has a single argument that takes an int. So the closest constructor is the one that takes a const char*. You are now initializing std::string using the const char* constructor, and that constructor requires that whatever is pointed to be terminated with a NULL. The string class may now attempt to dereference a NULL pointer when performing this operation, and doing that is undefined behaviour.

    So the string should be initialized to an empty string, not NULL.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 12th, 2011 at 09:52 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: [RESOLVED] Pointer to an array of pointers to a structure

    Quote Originally Posted by Amleto
    seems a very bad idea unless it's going to be a singleton, surely?
    We're dealing with an array of const objects, so there is no global state to be encapsulated into a singleton.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured