Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. extend class and casting

extend class and casting

Scheduled Pinned Locked Moved Managed C++/CLI
helplounge
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andreoli Carlo
    wrote on last edited by
    #1

    Basically i have 2 class (extended_1_GrObject and extended_2_GrObject) that derive from a general abstract class(basicGrObject):

    public ref class basicGrObject abstract
    {
    public:
    	basicGrObject(){}
    	/*
    	* Derived class must implement this function
    	*/
    	virtual void calculate() abstract;
    protected:
    	~basicGrObject(){}
    };
    
    public ref class extended_1_GrObject : public basicGrObject
    {
    public:
    	extended_1_GrObject()
    	{
    		// my code 
    	}
    	virtual void calculate() override
    	{
    		// my code 
    	}
    protected:
    	~extended_1_GrObject()
    	{
    		// my code 
    	}
    };
    public ref class extended_2_GrObject : public basicGrObject
    {
    public:
    	extended_2_GrObject()
    	{
    		// my code 
    	}
    	virtual void calculate() override
    	{
    		// my code 
    	}
    protected:
    	~extended_2_GrObject()
    	{
    		// my code 
    	}
    };
    

    In my code i create a list of this object and foreach element i have to call the calculate method

    System::Collections::ArrayList ^my_list=gcnew System::Collections::ArrayList();
    
    extended_1_GrObject ^extended_cl_1=gcnew extended_1_GrObject();
    my_list->Add(extended_cl_1);
    extended_2_GrObject ^extended_cl_2=gcnew extended_2_GrObject();
    my_list->Add(extended_cl_2);
    //doing this many time
    
    //other sample code
    for (int i=0;i<my_list->Count;i++) {
    	
    	//by now my bad bad way of calling the same method is
    	if (my_list[i]->GetType()->Name=="extended_1_GrObject" ) {
    		extended_1_GrObject ^tmp=static_cast<extended_1_GrObject^>(my_list[i]);
    		tmp->calculate();
    	}
    	else if (my_list[i]->GetType()->Name=="extended_2_GrObject" ) {
    		extended_2_GrObject ^tmp=static_cast<extended_2_GrObject^>(my_list[i]);
    		tmp->calculate();
    	}
    }
    

    i really don't like to do the ' my_list[i]->GetType()->Name=="extended_1_GrObject" ' is there some way to avoid that problem, my real class have list of 10000 elements and 10000 string comparison are very very heavy

    modified on Thursday, March 18, 2010 7:29 AM

    T 1 Reply Last reply
    0
    • A Andreoli Carlo

      Basically i have 2 class (extended_1_GrObject and extended_2_GrObject) that derive from a general abstract class(basicGrObject):

      public ref class basicGrObject abstract
      {
      public:
      	basicGrObject(){}
      	/*
      	* Derived class must implement this function
      	*/
      	virtual void calculate() abstract;
      protected:
      	~basicGrObject(){}
      };
      
      public ref class extended_1_GrObject : public basicGrObject
      {
      public:
      	extended_1_GrObject()
      	{
      		// my code 
      	}
      	virtual void calculate() override
      	{
      		// my code 
      	}
      protected:
      	~extended_1_GrObject()
      	{
      		// my code 
      	}
      };
      public ref class extended_2_GrObject : public basicGrObject
      {
      public:
      	extended_2_GrObject()
      	{
      		// my code 
      	}
      	virtual void calculate() override
      	{
      		// my code 
      	}
      protected:
      	~extended_2_GrObject()
      	{
      		// my code 
      	}
      };
      

      In my code i create a list of this object and foreach element i have to call the calculate method

      System::Collections::ArrayList ^my_list=gcnew System::Collections::ArrayList();
      
      extended_1_GrObject ^extended_cl_1=gcnew extended_1_GrObject();
      my_list->Add(extended_cl_1);
      extended_2_GrObject ^extended_cl_2=gcnew extended_2_GrObject();
      my_list->Add(extended_cl_2);
      //doing this many time
      
      //other sample code
      for (int i=0;i<my_list->Count;i++) {
      	
      	//by now my bad bad way of calling the same method is
      	if (my_list[i]->GetType()->Name=="extended_1_GrObject" ) {
      		extended_1_GrObject ^tmp=static_cast<extended_1_GrObject^>(my_list[i]);
      		tmp->calculate();
      	}
      	else if (my_list[i]->GetType()->Name=="extended_2_GrObject" ) {
      		extended_2_GrObject ^tmp=static_cast<extended_2_GrObject^>(my_list[i]);
      		tmp->calculate();
      	}
      }
      

      i really don't like to do the ' my_list[i]->GetType()->Name=="extended_1_GrObject" ' is there some way to avoid that problem, my real class have list of 10000 elements and 10000 string comparison are very very heavy

      modified on Thursday, March 18, 2010 7:29 AM

      T Offline
      T Offline
      teejayem
      wrote on last edited by
      #2

      This should work for you (note: psuedo code).

      extended_1_GrObject ^extended_cl_1=gcnew extended_1_GrObject();
      my_list->Add(extended_cl_1);
      extended_2_GrObject ^extended_cl_2=gcnew extended_2_GrObject();
      my_list->Add(extended_cl_2);

      //other sample code
      for (int i=0;i<my_list->Count;i++) {

      //by now my bad bad way of calling the same method is
      //if (my\_list\[i\]->GetType()->Name=="extended\_1\_GrObject" ) {
      //	extended\_1\_GrObject ^tmp=static\_cast<extended\_1\_GrObject^>(my\_list\[i\]);
      //	tmp->calculate();
      //}
      //else if (my\_list\[i\]->GetType()->Name=="extended\_2\_GrObject" ) {
      //	extended\_2\_GrObject ^tmp=static\_cast<extended\_2\_GrObject^>(my\_list\[i\]);
      //	tmp->calculate();
      //}
      
          basicGrObject ^tmp = static\_cast<basicGrObject^>(my\_list\[i\]);
          tmp->calculate();
      

      }

      Don't be overcome by evil, but overcome evil with good

      A 1 Reply Last reply
      0
      • T teejayem

        This should work for you (note: psuedo code).

        extended_1_GrObject ^extended_cl_1=gcnew extended_1_GrObject();
        my_list->Add(extended_cl_1);
        extended_2_GrObject ^extended_cl_2=gcnew extended_2_GrObject();
        my_list->Add(extended_cl_2);

        //other sample code
        for (int i=0;i<my_list->Count;i++) {

        //by now my bad bad way of calling the same method is
        //if (my\_list\[i\]->GetType()->Name=="extended\_1\_GrObject" ) {
        //	extended\_1\_GrObject ^tmp=static\_cast<extended\_1\_GrObject^>(my\_list\[i\]);
        //	tmp->calculate();
        //}
        //else if (my\_list\[i\]->GetType()->Name=="extended\_2\_GrObject" ) {
        //	extended\_2\_GrObject ^tmp=static\_cast<extended\_2\_GrObject^>(my\_list\[i\]);
        //	tmp->calculate();
        //}
        
            basicGrObject ^tmp = static\_cast<basicGrObject^>(my\_list\[i\]);
            tmp->calculate();
        

        }

        Don't be overcome by evil, but overcome evil with good

        A Offline
        A Offline
        Andreoli Carlo
        wrote on last edited by
        #3

        Sometimes looking for something simple is the best way...thank you :thumbsup:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups