本文整理汇总了C++中MyClass::setValue方法的典型用法代码示例。如果您正苦于以下问题:C++ MyClass::setValue方法的具体用法?C++ MyClass::setValue怎么用?C++ MyClass::setValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MyClass
的用法示例。
在下文中一共展示了MyClass::setValue方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
void main()
{
{
cout << endl << "unique_ptr for one Object" << endl;
unique_ptr<MyClass> ap1(new MyClass(101));
cout << "ap->getVal=" << ap1->getVal() << endl;
unique_ptr<MyClass> ap2;
ap2 = unique_ptr<MyClass>(new MyClass(102));
cout << "ap2->getVal=" << ap2->getVal() << endl;
MyClass *p = ap2.get();
p->setValue(122);
cout << "p->getVal=" << p->getVal() << endl;
cout << "ap2->getVal=" << ap2->getVal() << endl;
ap2.reset(new MyClass(132));
cout << "after Reset ap2->getVal=" << ap2->getVal() << endl;
p = ap2.release();
p->setValue(132);
cout << "p->getVal=" << p->getVal() << endl;
if (ap2)
cout << "ap2->getVal=" << ap2->getVal() << endl;
else
cout << "ap2 was deleted" << endl;
}
{
cout << endl << "unique_ptr in Vector" << endl;
vector<unique_ptr<MyClass>> v;
v.push_back(unique_ptr<MyClass>(new MyClass(3)));
v.push_back(unique_ptr<MyClass>(new MyClass(5)));
v.push_back(unique_ptr<MyClass>(new MyClass(2)));
v.push_back(unique_ptr<MyClass>(new MyClass(1)));
v.push_back(unique_ptr<MyClass>(new MyClass(4)));
v.push_back(unique_ptr<MyClass>(new MyClass(2)));
for (unsigned int i=0 ; i<v.size() ; i++)
cout << "getVal=" << v[i]->getVal() << endl;
sort(v.begin(), v.end(), orderMyClass);
cout << endl << "After sorting:" << endl;
for (unsigned int i=0 ; i<v.size() ; i++)
cout << "getVal=" << v[i]->getVal() << endl;
}
{
cout << endl << "unique_ptr returned from Factory" << endl;
unique_ptr<MyClass> o = Factory(7);
cout << "returned from Factory=" << o->getVal() << endl;
}
{
cout << endl << "unique_ptr in Vector with Polymorphism" << endl;
vector<unique_ptr<BaseClass>> v;
v.push_back(unique_ptr<BaseClass>(new YourClass(3)));
v.push_back(unique_ptr<BaseClass>(new MyClass(6)));
v.push_back(unique_ptr<BaseClass>(new YourClass(5)));
v.push_back(unique_ptr<BaseClass>(new YourClass(1)));
v.push_back(unique_ptr<BaseClass>(new MyClass(4)));
v.push_back(unique_ptr<BaseClass>(new MyClass(2)));
for (unsigned int i=0 ; i<v.size() ; i++)
cout << "getVal=" << v[i]->getVal() << endl;
sort(v.begin(), v.end(), orderBaseClass);
/* ERROR when used with unique_ptr
// replace_if(v.begin(), v.end(), MyClass::EqualTo, unique_ptr<MyClass>(new MyClass(0)));
*/
cout << endl << "After sorting:" << endl;
for (unsigned int i=0 ; i<v.size() ; i++)
cout << "getVal=" << v[i]->getVal() << endl;
}
}