Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Intel(R) C++ Composer XE 2013 for Windows can not support polymorphism

Qingfeng_W_
Beginner
646 Views
0 Kudos
7 Replies
Qingfeng_W_
Beginner
646 Views

My compile environment:Intel(R) C++ Composer XE 2013 for Windows(ver.2013.0.0.89) and visual studio 2008.

My problem:When my code enter the destructor, it straight into the destructor of base class instead of the derived class.

I want to know how to deal the problem of Intel compiler,the visual studio compiler has no same problem.

0 Kudos
Sukruth_H_Intel
Employee
646 Views

Hi,

      I am not able to reproduce the case that you are mentioning with latest 14.0 Intel compiler and even with Intel compiler 13.1.1.171 Build 20130313 and 13.1.3.198 Build 20130607. Let's take an example :-

#include<iostream>

using namespace std;

class A
{

public:    A()
    {
        cout<<"Constructor from class A is been called "<<endl;
    }

~A()
{
    cout<<"Destructor from class A is been called "<<endl;
}    

};

class B : A
{

public:
    B()
    {
        cout<<"Constructor from class B is been called "<<endl;
    }

    ~B()
    {
        cout<<"Destructor from class B is been called "<<endl;
    }
};

int main()
{
    B b;
    return 0;
}

//command line used :-

C:\Users\shv\Desktop>icl simple.cpp

C:\Users\shv\Desktop>simple.exe

Constructor from class A is been called
Constructor from class B is been called
Destructor from class B is been called
Destructor from class A is been called

The output is as expected with both Intel as well as Microsoft* compiler. Could you please update your compiler and see if the issue still persists?

Regards,

Sukruth H V 

0 Kudos
Qingfeng_W_
Beginner
646 Views

sukruth-v (Intel) wrote:

Hi,

      I am not able to reproduce the case that you are mentioning with latest 14.0 Intel compiler and even with Intel compiler 13.1.1.171 Build 20130313 and 13.1.3.198 Build 20130607. Let's take an example :-

#include<iostream>

using namespace std;

class A

{

public:    A()

    {

        cout<<"Constructor from class A is been called "<<endl;

    }

~A()

{

    cout<<"Destructor from class A is been called "<<endl;

}  

};

class B : A

{

public:

    B()

    {

        cout<<"Constructor from class B is been called "<<endl;

    }

    ~B()

    {

        cout<<"Destructor from class B is been called "<<endl;

    }

};

int main()

{

    B b;

    return 0;

}

//command line used :-

C:\Users\shv\Desktop>icl simple.cpp

C:\Users\shv\Desktop>simple.exe

Constructor from class A is been called

Constructor from class B is been called

Destructor from class B is been called

Destructor from class A is been called

The output is as expected with both Intel as well as Microsoft* compiler. Could you please update your compiler and see if the issue still persists?

Regards,

Sukruth H V

Thanks for help !

I wil try for the new version of intel compiler.

Best Regards.

0 Kudos
Qingfeng_W_
Beginner
646 Views

Very disappointed! The  problem still exists.

My intel compiler version: Intel(R) C++ Compiler XE 14.0.3.202.

My code:

#include "stdafx.h"
#include<iostream>
using namespace std;

class Base
{
public:
 Base()
 {
  cout<<"Constructor from class Base is been called "<<endl;
 }
 virtual~Base()
 {
  cout<<"Destructor from class Base is been called "<<endl;
 }
};
class Derived : public Base
{
public:
    Derived()
 {
  cout<<"Constructor from class Derived is been called "<<endl;
 }
 virtual ~Derived()
 {
  cout<<"Destructor from class Derived is been called "<<endl;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 Base* pTemp = new Derived[2];
 delete []pTemp;
 system("pause");
 return 1;
}

My output:

Constructor from class Base is been called
Constructor from class Derived is been called
Constructor from class Base is been called
Constructor from class Derived is been called
Destructor from class Base is been called
Destructor from class Base is been called

Please help me.

Best Regards.

0 Kudos
Sukruth_H_Intel
Employee
646 Views

Hi,

      I was able to reproduce the error and i have escalated the same to our engineering team and would get back to you with an update soon.

Regards,
Sukruth H V

0 Kudos
Sukruth_H_Intel
Employee
646 Views

Hi,

     Our engineering team conveys that the program behavior deviates slightly from standards, What they mean by this is :-

This declaration " Base* pTemp = new Derived[2];" 

Reason being that your pointer is of type "Base" and the allocated object is of type "Derived", However when you destruct, you are using the wrong type of pointer, which is of type "Base".

Let's consider that the derived class might have more data members than the base class, so it might be larger. When the array object is deleted, it is necessary to destroy each element of the array. In order to know where each element is, you first have to know how big each element is. You have to determine the size of the array elements at run-time. 

So i have tested by modifying the allocation as "Derived* pTemp = new Derived[2];" and it works as expected.

However i have requested our engineering team to take this as a feature request and they have considered the same.

Please do let me know if you need any further clarification on this issue.

Regards,
Sukruth H V

 

 

 

0 Kudos
Bernard
Valued Contributor I
646 Views

 >>>When the array object is deleted, it is necessary to destroy each element of the array. In order to know where each element is, you first have to know how big each element is. You have to determine the size of the array elements at run-time. >>>

IIRC delete operator[] at the compile time will calculate the array size.

0 Kudos
Reply