怀疑回报类型 [英] doubt in return types

查看:73
本文介绍了怀疑回报类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CXClass,它来自下面提到的CXClass1和CXClass2:



类CXClass:public CXClass1,public CXClass2



CXClass1(我有两个变量,即X和y变量),因为它是2D类。



CXClass2(我有两个变量,即X,y和z变量)因为它是3D类。



我在两个类中都有GetX()(返回X值)API(以及两个x类的值) x可能是不同的)



主要派生类CXClass GetX()在下面提到的调用:



double CXClass :: GetX()

{

//在此我必须返回GetX的CXClass1和CXClass2 APis()



}



以下是样本:



< pre lang =C#> class CXClass1
{
public
int m_i;
int m_j;

CXClass1(){cout<< 我是Derived1构造函数<< ENDL; }


void print(){cout<< 我是衍生1<< ENDL; }

void func1()
{
cout<< 我是Derived1 func1<< ENDL;
}

double GetX()
{
m_i = 3 ;
return m_i;
}
};



class CXClass2
{
public
int m_i;
int m_j;
int m_k;


CXClass2(){cout<< 我是Derived2构造函数<< ENDL; }
void print(){cout<< 我是衍生2<< ENDL; }


void func2()
{
cout<< 我是CDerived2 func2<< ENDL;
}


double GetX()
{
m_i = 2 ;
return m_i;
}
};

class CXClass: public CXClass1, public CXClass2
{
public

CXClass()
{

}


double GetX()
{

double d = CXClass1 :: GetX();
double k = CXClass2 :: GetX();

// 我必须返回两个api值?
}

};


void main()
{
CXClass obj;
double d = obj.GetX();

}





请给出你的suugestions



谢谢

解决方案

简短的答案和坏消息是,如果你只能从一个函数返回一个值。好消息是你可以返回任何有价值的东西,这几乎是任何东西。所以你可以写一些像...



  return  std: :make_pair(class_1 :: X(),class_2 :: X()); 





得到你所要求的,即使你真的不想这样做。为什么不?继续阅读...



公共继承只有在派生类的对象可以替换您使用基类实例的所有位置时才有意义。所以这意味着你写的东西到处都是:



  void  frob( base_class& b); 

int main()
{
base_class b;
frob(b);
}



您可以改写:



 < span class =code-keyword> int  main()
{
derived_class d;
frob(d);
}



程序仍然有效。程序不必具有相同的行为(否则无法继承)但它仍然必须编译并运行而不会出现错误。它是OO设计的基石之一。



在你的情况下,你说无论我在我的代码中使用base_class_1或base_class_2,我希望能够替换derived_class对象,程序仍然有效但我希望我的几个函数的返回类型返回不同的东西,所有客户端代码仍然有效。这是一个很大的问题,所以我建议你查看你的代码并看看它是否真的是最好的主意(提示:它不是)。



问题听起来像你'有两个相似的概念,可能共享一大堆代码。不知道你的代码或描述中的概念是什么,但它们似乎是依赖于坐标系统的东西。我建议你不要乱用多重继承,而是寻找坐标系的抽象,以及它表示和操纵点的方式,并在现在的派生类中进行参数化,例如:类似于:



 template< class coordinate_system> 
class graphical_vector
{
public
graphical_vector (coordinate_system :: point_type& start,coordinate_system :: point_type& end):start_(start),end_(end){};

double length() const
{
return distance_from(start_,end_);
};

private
const coordinate_system :: point_type start_;
const coordinate_system :: point_type end_;
};





因为我不知道你写的是什么类型的应用程序我不知道这是不是任何成功的机会。通常情况下,当我看到多种继承被用于提倡这种解决方案的方式更好。



如果这是完整的球,请随时澄清你的通过告诉我们你想要实现的目标以及原因来提出一些问题(实际上很多)。可能会发现你有一个非常酷的,有效的用例,它会告诉我们其他人一些东西并改进我们的工作,或者我们可能会建议其他方式让你更好地为你和你的代码从长远来看。


I have CXClass which is derived from CXClass1 and CXClass2 mentioned below:

class CXClass : public CXClass1 , public CXClass2

CXClass1(i have two variables i.e X and y variables) because it is 2D class.

CXClass2(i have two variables i.e X ,y and z variables) because it is 3D class.

I have GetX() ( to return X value) API in both the classes ( and both the x classes values of x might be different)

somewhere in the main the derived class CXClass GetX() is called mentioned below:

double CXClass::GetX()
{
// in this i have to return both the CXClass1 and CXClass2 APis of GetX()

}

below is the sample:

class CXClass1
{
public:
	int m_i;
	int m_j;
	
	CXClass1() { cout << "I am Derived1 Constructor" << endl; }


	void print() { cout << " I am derived1" << endl; }

	void func1()
	{
		cout << "I am Derived1 func1" << endl;
	}

	 double GetX()
	{
		m_i = 3;
		return m_i;
	}
};



class CXClass2
{
public:
	int m_i;
	int m_j;
	int m_k;


	CXClass2() { cout << "I am Derived2 Constructor" << endl; }
	void print() { cout << " I am derived2" << endl; }

	
	void func2()
	{
		cout << "I am CDerived2 func2" << endl;
	}


	double GetX()
	{
		m_i = 2;
		return m_i;
	}
};

class CXClass : public  CXClass1, public  CXClass2
{
public:

	CXClass()
	{

	}


	double GetX() 
	{
		
		double d = CXClass1::GetX();
		double k = CXClass2::GetX();

		//i have to return two apis values ?
	}

};


void main()
{
	CXClass obj;
	double d = obj.GetX();
	
}



Please give ur suugestions

Thanks

解决方案

The short answer and bad news is that if you can only return a single value from a function. The good news is that you can return anything that's a value, which is just about anything. So you could write something like...

return std::make_pair( class_1::X(), class_2::X() );



and get what you asked for, even though you don't actually want to do that. Why not? Read on...

Public inheritance only really makes sense where objects of the derived class can be substituted for all places you use instances of the base class. So this means everywhere you write something like:

void frob( base_class &b );

int main()
{
    base_class b;
    frob( b );
}


you can instead write:

int main()
{
    derived_class d;
    frob( d );
}


and the program is still valid. The program doesn't have to have the same behaviour (there'd be no point to inheritance otherwise) but it must still compile and run without error. It's one of the cornerstones of OO design.

In your case you're saying "wherever I use a base_class_1 OR a base_class_2 in my code I want to be able to substitute a derived_class object and the program still remain valid but I want the return types of several of my functions to return something different and all the client code still be valid." That's a big ask so I'd advise looking at your code and seeing if it's really the best idea (HINT: It's not).

The problem sounds like you've got two concepts that are similar and presumably share a fair chunk of code. No idea what the concepts are from you code or description but they seem to be things that rely on a coordinate system. Instead of messing about with multiple inheritance what I'd suggest you look for an abstraction for the coordinate system and the way it represents and manipulates points and parameterise that in what is now your derived class, e.g. something like:

template<class coordinate_system>
class graphical_vector
{
    public:
        graphical_vector( coordinate_system::point_type &start, coordinate_system::point_type &end ): start_(start), end_(end){};

        double length() const
        {
            return distance_from( start_, end_ );
        };

    private:
        const coordinate_system::point_type start_;
        const coordinate_system::point_type end_;
};



As I have no idea of what sort of app you're writing I have no idea if this stands any chance of success. Fairly often when I've seen multiple inheritance being used in the way you advocate this sort of solution works better.

If this is complete balls please feel free to clarify your question a bit (actually a lot) by telling us what you're trying to achieve and why. It may turn out you've got a really cool, valid use case that'll tell the rest of us something and improve what we do or we might be able to suggest other ways for you to go that are better for you and your code in the long run.


这篇关于怀疑回报类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆