我想知道这是否是c++中多态的一个例子。我检查了OOP中的示例,但没有找到任何纯过程方式的编程。提前感谢您的回答!
#include <iostream>
using namespace std;
int return_value(int a) {
return 5;
}
char return_value(char b) {
return 'a';
}
int main() {
int a = 1;
char b = 'c';
cout << return_value(a);
cout <<
我有一个与市场中心模型有多态关系的模型团队:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
public function teamable()
{
return $this->morphTo();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Marketcenter extends Mod
我想在我的类中使用函数多态性。我打算检查一些子类函数的函数的输入类型。
class A
{
public function setValue(Type $value)
{
}
}
class B extends A
{
public function setValue(XType $value)
{
}
}
class C extends A
{
public function setValue(ZType $value)
{
}
}
$b = new B();
$b->setValue(new XType())
module MapHelpers (Ord : Map.OrderedType) = struct
include Map.Make (Ord)
let add_all a b = fold add a b
end
工作,但看似等同
module MapHelpers (Ord : Map.OrderedType) = struct
include Map.Make (Ord)
let add_all = fold add
end
无法使用
File "Foo.ml", line 2, characters 18-104:
Error: The type of
对于instance Alternative [],(<|>) = (++)。因此,我认为(<|>)是一种拼接器,导致了看似几乎通用的容器转换器:
-- (<|>) = generalization of (++)
(<|) :: Alternative f => x -> f x -> f x
x <| xs = pure x <|> xs
conv :: (Foldable t, Alternative f) => t x -> f x
conv = foldr (<|) empty
实际上
我想了解虚拟函数的用途。
让我们分析一下成员函数是非虚函数的代码:
示例1:
struct A
{
void foo1() { cout << "foo 1 from A \n"; }
};
struct B : A
{
void foo1() { cout << "foo 1 from B \n"; }
};
int main()
{
A *a = new B;
a->foo1(); // will print "foo 1 from A \n" because A::foo1
我试图找出类型类的用途,如果不使用类型类,还有什么。
类型类是定义多态函数的一种方法吗?
类型类是定义多态函数的唯一方法吗?例如:
class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)
instance Eq Bool where
False == False = True
True == True = True
_ == _ = False
我可以在不使用类型类==的情况下为Bool (和其他类型)定义Bool和Eq
在一些书中,声明或继承虚函数的类被称为多态类。
类B没有任何虚函数,但通过了多个is-a测试。
类C有一个虚函数,但没有继承。
class A {};
class B : public A {};
class C
{
public:
virtual void f () {}
};
B类或C类是多态的吗?
例如:
class Animal
{
virtual void dummy() {}; //LINE1
}
class Cat : public Animal
{
}
Animal* a = new Cat();
if (Cat* c = dynamic_cast<Cat*> (a)) //LINE2
{
//Do something.
}
如果我从动物类中删除LINE1 (即动物类不包含虚拟成员),LINE2将无法工作。
谁能给我解释一下为什么这段代码打印Base,Derived,但是如果我省略了Base中的f函数,就会打印Base,Base?
#include <iostream>
#include <cstdio>
using namespace std;
class Base;
void testClassType (Base& b);
class Base
{
virtual void f(){};
};
class Derived :public Base
{
};
int main ()
{
Base b;
Derived d;
testClassTy
我有一个继承自A的类B。
class A
{
};
class B : public A
{
};
我有三个对象。
A* a = new A();
A* a2 = new B();
B* b = new B();
我想知道,如果检查A是A类型的对象,a2是B类型(不是A)的对象,b是B类型的对象。
我试过打字比较,但没有给出正确的答案。
cout << (typeid(*a) == typeid(A)) << endl; // -> 1
cout << (typeid(*a2) == typeid(A)) << endl; // ->
我在试着理解为什么下面的一个是标准允许的,而另一个是不允许的。对我来说,除了样板代码之外,它们似乎没有什么不同。我觉得我误解了什么,或者有更好的方法去做。任何帮助都将不胜感激。
不允许:
real :: x
class(*) :: temp
x = 4
temp = genericAssignment(x)
select type(temp)
type is(real)
write(*,*) temp
end select
contains
function genericAssignment(a) result(b)
class(*) :: a
class(
在下面的示例中,我必须重申,M是一个Monad,以便定义bar,这似乎是不必要的,因为我已经告诉了编译器,因为我定义了fnc。
import scalaz._
import scalaz.Scalaz._
object MyTest {
// function returning a function accepting a monad as parameter
def fnc[M[_]: Monad, T](t: T): (M[T] => T) = xs => t
// this works
def bar[M[_]: Monad]: (M[Double]
对于具有虚拟函数的bass类,通常称为“多态基类”。在本例中,Car类
class Car
{
virtual void drive(){std::cout << "drive right";}
};
class EuropeCar: public Car
{
void drive(){std::cout << "drive left";}
}
polymorphic base class是一般俚语,如pure abstract class,还是C++标准的一部分?
我遇到了一个我无法通过的问题,下面是我的模特:
Cloth.php
public function selling(): BelongsTo
{
return $this->belongsTo(Selling::class);
}
Selling.php
public function clothes(): HasMany
{
return $this->hasMany(Cloth::class);
}
,现在一切都好了,很基本.但随后出现了这样的模式:
Accessory.php
public function selling(): BelongsTo
{