CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2012
    Posts
    38

    Factory method and my mock object

    I have a class Customer I create a CustomerFactory for it but I would also like to include a Customer_Mock. So I think my factory will create a customer_mock instead of a real customer. Here is what I have done. But I am kept reminded of link error *.ctor as unresolved token and unresolved external symbol*. I am wrong about something I don't have an idea how to fix it myself. Thank you

    PHP Code:
    using namespace System;

    ref class Customer
    {
        
    int age;
        
    StringName;
    public:
        
    Customer();
        
    Customer(int aStringn)
        {
            
    Name=n;age=a;
        }
        
    virtual void Func()
        {
            
    Console::WriteLine(L"Customer Func");
        }
    };

    ref class Customer_Mock:Customer
    {
        
    int age;
        
    StringName;
    public:
        
    Customer_Mockint age,String^name){this->age=age;this->Name=name;}
        
    virtual void Func() override
        
    {
            
    Console::WriteLine(L"Mock is called");
        }
    };

    ref class CustomerFactory
    {    
        
    CustomerFactory(){Console::WriteLine(L"Customer Factory Constructor");}

    public:    
        static 
    Customer_MockCreateCustomer(int age,String^name )
        {
            return 
    gcnew Customer_Mock(age,name);
        }    
    };


    int main(array<System::String ^> ^args)
    {
        
    Console::WriteLine(L"Hello World");
        
    Customer^c=CustomerFactory::CreateCustomer(10,L"simpson");
        
    c->Func();
        return 
    0;


  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Factory method and my mock object

    Quote Originally Posted by terminalXXX View Post
    Code:
    ref class Customer
    {
    	int age;
    	String^ Name;
    public:
    	Customer();
    	Customer(int a, String^ n)
    	{
    		Name=n;age=a;
    	}
    	virtual void Func()
    	{
    		Console::WriteLine(L"Customer Func");
    	}
    };
    You are declaring a Customer default constructor, but you aren't implementing it, hence the linker error. You probaly have already seen, though, that you get a compiler error if you don't declare it. That's because Customer already has a constructor that takes parameters, so the compiler doesn't default-create a default constructor. As a workaround, you may implement the default constructor as a no-op:

    Code:
    	Customer() {}
    However, that's more sort of a hack here, there's a better option: Make use of the two-parameter base class constructor when you construct your Customer_Mock object:

    Code:
      Customer_Mock(int age, String ^name) : Customer(age, name)
      {}
    That way you don't need a Costomer default costructor at all here.

    And that leads us to another problem with your original code: You are duplicating the two private data members of Customer in your mock class. In your original code, you'd initialize the duplicate members in the mock class, while the base class members remain uninitialized. (My proposed solution of using the base class cosntructor in the mock class construction would initialize the base class members but leave the duplicates in the mock class uninitialized, but that will be cleared in a moment.) That way, to get something that "works", you'd need to override each and every member function of Customer that accesses the member variables in your mock class and duplicate its code, so it accesses the duplicate data members of the mock class. That wouldn't only be a real PITA with non-trivial real-life classes, it also would be a mighty magnet for bugs introduced by failure to properly replicate code changes between the two versions of the code.

    Instead, simply make the base class data members protected rather than private, so member functions of derived classes are allowed to access them. That way you only need to override base class functions for which this is really necessary. This is not only much simpler and safer, it's the natural way things like this work.

    The proposed code changes in total:

    Code:
    ref class Customer
    {
    protected:
      int age;
      String ^Name;
    
    public:
      Customer(int a, String ^n) : Name(n), age(a)
      {}
    
      virtual void Func()
      {
        Console::WriteLine("Customer Func");
      }
    };
    
    ref class Customer_Mock : public Customer
    {
    public:
      Customer_Mock(int age, String ^name) : Customer(age, name)
      {}
    
      virtual void Func() override
      {
        Console::WriteLine("Mock is called");
      }
    };
    Finally, please don't use PHP tags for C++[/CLI] code, use real code tags. You may like how colorful that looks, but the PHP syntax highlighting isn't really appropriate for C++ code.
    Last edited by Eri523; June 16th, 2013 at 07:19 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jun 2012
    Posts
    38

    Re: Factory method and my mock object

    Thank you Eric, that is awesome!
    I use php tag only for clarity purpose. Others I don't care.

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