hi all, I get a compile time error when I tried to assign A*
to A[]. I get the error in DEV C++ , mingw ,error
"incompatible type assignment"

should I have to use dirty pointers for this ?

#include <iostream>
 
class A
{
public:
       int a ;
       int b ;
       static int c ;
       /* default constructor.
       */
       A()
       {
                  c++ ;
                  std::cout << " A() is called " << " and a is " << c << std::endl;
       }
       
       /* assignment operator 
       */
       const A& operator = ( const A& a )
       {
             std::cout << " assiment operator is called " << std::endl ;
              
             
       }
       
       /* The destructor
       */
       ~A()
       {
           std::cout << " ~A() is called " << std::endl ;
       }
       
private:
      
};
 int A::c=0 ;


class B
{
public:      
      A  a[];
      
      /*
        default constructor
      */
      B()
      {
                std::cout << " B() is called " << std::endl ;
                a = new A[200];
       }
      
      /* 
         THe destructor of the B
         */
         ~B()
         {
             std::cout << " ~B() is called " << std::endl ;
         }
};

int main(int argc, char *argv[])
{
    B b ;
    delete &b;       
    std::getchar() ;
    return EXIT_SUCCESS;
}

Recommended Answers

All 10 Replies

hi all, I get a compile time error when I tried to assign A*
to A[]. I get the error in DEV C++ , mingw ,error
"incompatible type assignment"

should I have to use dirty pointers for this ?

#include <iostream>
 
class A
{
public:
       int a ;
       int b ;
       static int c ;
       /* default constructor.
       */
       A()
       {
                  c++ ;
                  std::cout << " A() is called " << " and a is " << c << std::endl;
       }
       
       /* assignment operator 
       */
       const A& operator = ( const A& a )
       {
             std::cout << " assiment operator is called " << std::endl ;
              
             
       }
       
       /* The destructor
       */
       ~A()
       {
           std::cout << " ~A() is called " << std::endl ;
       }
       
private:
      
};
 int A::c=0 ;


class B
{
public:      
      A  a[];
      
      /*
        default constructor
      */
      B()
      {
                std::cout << " B() is called " << std::endl ;
                a = new A[200];
       }
      
      /* 
         THe destructor of the B
         */
         ~B()
         {
             std::cout << " ~B() is called " << std::endl ;
         }
};

int main(int argc, char *argv[])
{
    B b ;
    delete &b;       
    std::getchar() ;
    return EXIT_SUCCESS;
}

try using *a in place of a[].
i don't think a declaration such as A a[] is valid.

Why don't you use a 'vector' class instead of arrays? It would be much easier to use.

2 things:

a) Like someone already stated, replace A a[] with A* a and it will compile and run just fine.

b) Don't forget to delete *a in your destructor

And

c) Make sure you have copy ctor
d) Define a proper assignment operator for your class

I would still advise you to use a vector unless you are bound to using a raw array. Read here for more info vector c++ reference

I would still advise you to use a vector unless you are bound to using a raw array. Read here for more info vector c++ reference

I need to pass that data structures between multiple dll modules. So
there are initialization problems. So I can't use the std::vector :(.
But I think I can write my own wrapper class to encapsulate it if
C++ does not support this in the language level.

I need to pass that data structures between multiple dll modules. So
there are initialization problems. So I can't use the std::vector :(.
But I think I can write my own wrapper class to encapsulate it if
C++ does not support this in the language level.

Correct me if I'm wrong, but couldn't you just....

#include <iostream>
#include <iomanip>

using std::cout;
using std::setw;

class A
{
	static int i;
public:
	A() { cout << setw(3) << i << ". A()\n"; i++; }
	~A() { i--; cout << setw(3) << i << ". ~A()\n"; }
};

int A::i = 1;

class B
{
	enum { sz = 10 };
	A* a;
public:
	B() { a = new A[sz]; }
	~B() { delete []a; }
};

int main()
{
	B b;
}

...do it like so? (Add your own copy constructors, and whatever else you might need)

Correct me if I'm wrong, but couldn't you just....

#include <iostream>
#include <iomanip>

using std::cout;
using std::setw;

class A
{
	static int i;
public:
	A() { cout << setw(3) << i << ". A()\n"; i++; }
	~A() { i--; cout << setw(3) << i << ". ~A()\n"; }
};

int A::i = 1;

class B
{
	enum { sz = 10 };
	[B]A* a;[/B]
public:
	B() { a = new A[sz]; }
	~B() { delete []a; }
};

int main()
{
	B b;
}

...do it like so? (Add your own copy constructors, and whatever else you might need)

that is how I managed to make it compile. But it's syntactically
not fair that you can use A * as A[] but you can't assign a A* to
A[].

may be there is a reason for this , I want to know this reason , if
not I think it's a good suggestion to next release of C++ specification.Correct me if I am wrong.

And

c) Make sure you have copy ctor
d) Define a proper assignment operator for your class

and I also taught that this was a good idea. The idea of writing a
wrapper class. overload the

operator * and operator [] and operator new

But I feel more unnatural and guilty to do it.I feel that also not
syntactically fair.

I think that the key to your problem is that "new" returns a "pointer". So, if you do this:

A a[];
a = new A[10];

Then if I'm reading it right then you are saying:
1) 'a' is an "array" of objects
2) 'a' is a "pointer" to an array of objects
A pointer is not exactly the same as an array and therefore you're getting the error.

However, if you say:

A* a;
a = new A[10];

then you are saying:
1) 'a' is a pointer
2) 'a' is a pointer to an array of objects
so technically it sounds correct and it works (compiles). Then any time you use pointers in an object you should take care of destruction, copying, and assignment to make sure that everything works right in your program, but you'll know best what you need because your needs will vary depending on how you plan to use the object.

I'm only on my first C++ course right now so that's the best explanation that I can give.

right get it.

you telling that A[] is typed than A* that's why you can assign A[] to A* but not the reverse way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.