class CRefCounter { public: CRefCounter() { icount; } int iAdd() { return(++icount); }…
- User
class CRefCounter { public: CRefCounter() { i_count; } int iAdd() { return(++i_count); } int iDec() { return(--i_count); }; int iGet() { return(i_count); } private: int i_count; };//class CRefCounter The counter object is created in the smart pointer class. class CMySmartPointer { public: CMySmartPointer(CSellData *pcPointer) { „ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 3/8 pc_pointer = pcPointer; pc_counter = new CRefCounter(); pc_counter->iAdd(); }//CMySmartPointer(CSellData pcPointer) CMySmartPointer(const CMySmartPointer &pcOther) { pc_pointer = pcOther.pc_pointer; pc_counter = pcOther.pc_counter; pc_counter->iAdd(); }//CMySmartPointer(const CMySmartPointer &pcOther) ~CMySmartPointer() { if (pc_counter->iDec() == 0) { delete pc_pointer; delete pc_counter; }//if (pc_counter->iDec()) }//~CMySmartPointer() CSellData& operator() { return(pc_pointer); } CSellData operator->() { return(pc_pointer); } private: CRefCounter *pc_counter; CSellData *pc_pointer; };//class CMySmartPointer In the case of the above implementation, multiple smart pointers can store the same pointer and will have the same reference count. However, an error will not occur. The objects pointed to by pc_counter and pc_pointer will be deleted when the last smart pointer that stores them is deleted.
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 4/8 There are situations where we have a statically created object in some function and we want to pass the value of this object outside. This is a typical situation for operator overloading. For example: CNumber CNumber::operator+(CNumber &cNum) { CNumber c_result; /create result/ return(c_result); }//CNumber CNumber::operator+(CNumber &cNum) The above implementation of the addition operator is convenient because if the c_result object were dynamically allocated and the operator returned a pointer to CNumber instead of the value of CNumber, then if the result was not assigned to any variable outside, a leak would occur. At the same time, if the CNumber class allocates a lot of memory, then deleting c_result at the end of the operator can be considered wasteful because this object will not be used anywhere, and the memory it allocated could have been passed on to another object for use. Instead, the memory will be copied, which is time-consuming. To handle the above situations, we use move semantics. Consider the example of the class CTab. #define DEF_TAB_SIZE 10 class CTab { public: CTab() { pi_tab = new int[DEF_TAB_SIZE]; i_size = DEF_TAB_SIZE;} CTab(const CTab &cOther); CTab(CTab &&cOther); CTab operator=(const CTab &cOther); ~CTab(); bool bSetSize(int iNewSize); int iGetSize() { return(i_size); } private: void v_copy(const CTab &cOther); int *pi_tab; int i_size; };//class CTab
„ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 5/8 Selected methods of the CTab class. CTab::CTab(const CTab &cOther) { v_copy(cOther); std::cout << "Copy "; }//CTab::CTab(const CTab &cOther) CTab::~CTab() { if (pi_tab != NULL) delete pi_tab; std::cout << "Destr "; }//CTab::~CTab() CTab CTab::operator=(const CTab &cOther) { if (pi_tab != NULL) delete pi_tab; v_copy(cOther); std::cout << "op= "; return(*this); }//CTab CTab::operator=(const CTab &cOther) void CTab::v_copy(const CTab &cOther) { pi_tab = new int[cOther.i_size]; i_size = cOther.i_size; for (int ii = 0; ii < cOther.i_size; ii++) pi_tab[ii] = cOther.pi_tab[ii]; }//void CTab::v_copy(CTab &cOther) In the traditional way (with copying) we can use the class as in the following program. CTab cCreateTab() { CTab c_result; c_result.bSetSize(5); return(c_result); }//CTab cCreateTab() int i_ms_test() { CTab c_tab = cCreateTab(); // }//int i_ms_test() If the compiler does not optimize the code during compilation, the c_tab object will be created using the copy constructor and the array will be copied. However, we can define a move constructor with the following content.
- Assistant
- User
firstly do this use headers for each created class
- Assistant
- User
do only this tasks 1. Using the skills you learned in the previous labs, convert the CMySmartPointer class to a template class 2. Implement operator= so that one smart pointer can be assigned the value of another. Remember that if the smart pointer being modified pointed to or stored another pointer before being assigned, the reference count must be decremented and deallocated if necessary. 3. Consider what happens when a smart pointer stores a pointer to statically allocated memory.
- Assistant
- User
- Assistant
- User
my teacher used this as example Table operator+(const Table& rhs) const &; Table operator+(Table&& rhs) const &; Table operator+(const Table& rhs) &&; Table operator+(Table&& rhs) &&; so do not use noexcept
- Assistant
- User
In the part of the discussion regarding different cases for the operators, and by extent other functions as well, I have described the qualifiers one can add after the parameter list of a method (a class member function).
In the example, we had:
- Table operator+(const Table& rhs) const;
- Table operator+(Table&& rhs) const;
- Table operator+(const Table& rhs) &&;
- Table operator+(Table&& rhs) &&;
As stated during the presentation, the 4th case is not needed.
But, even if only the first three are enough this code won't compile. The compilation error that arises comes from a "conflict" between 1 both 2 and 3. The problem is that untill C++14, if one added the "&&" modifiers at the end, the compiler expected similar reference annotations everywhere! Because of that overloads 1 and 3 are treated as the same as the overload mechanism sees identical parameter lists (const Table& rhs).
To solve this problem, all of the overloads have to specify all of the "&&"and "&" qualifiers:
- Table operator+(const Table& rhs) const &;
- Table operator+(Table&& rhs) const &;
- Table operator+(const Table& rhs) &&;
- Assistant
- User
give whole files
- Assistant
- User
- Assistant
- User
int main() { Tree t1, t2; enter(t1); enter(t2); Tree t1_copy = t1; Tree t2_copy = t2; Tree t3;
std::cout << "t1 = " << t1.print() << '\n'; std::cout << "t2 = " << t2.print() << '\n'; std::cout << '\n'; t3 = t1 + t2; std::cout << "lvalue + lvalue: " + t3.print() << '\n'; std::cout << "t1 = " << t1.print() << '\n'; std::cout << "t2 = " << t2.print() << '\n'; std::cout << '\n'; t1 = t1_copy; t2 = t2_copy; t3 = t1 + std::move(t2); std::cout << "lvalue + rvalue: " + t3.print() << '\n'; std::cout << "t1 = " << t1.print() << '\n'; std::cout << "t2 = " << t2.print() << '\n'; std::cout << '\n'; t1 = t1_copy; t2 = t2_copy; t3 = std::move(t1) + t2; std::cout << "rvalue + lvalue: " + t3.print() << '\n'; std::cout << "t1 = " << t1.print() << '\n'; std::cout << "t2 = " << t2.print() << '\n'; std::cout << '\n'; t1 = t1_copy; t2 = t2_copy; t3 = std::move(t1) + std::move(t2); std::cout << "rvalue + rvalue: " + t3.print() << '\n'; std::cout << "t1 = " << t1.print() << '\n'; std::cout << "t2 = " << t2.print() << '\n'; std::cout << '\n'; t1 = t1_copy; t2 = t2_copy; auto t4 = std::move(t1); std::cout << "t1 = " << t1.print() << '\n'; std::cout << "t4 = " << t4.print() << '\n'; return 0;} do similar testing in main byt with my class
- Assistant
- User
- Assistant
- User
- Assistant
- User
CTree.cpp give whole
- Assistant
- User
||=== Build: Release in lab5 (compiler: GNU GCC Compiler) ===| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'CNode* CTree::deepCopy(const CNode*) const':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|80|error: passing 'const CNode' as 'this' argument discards qualifiers [-fpermissive]| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CNode.h|15|note: in call to 'std::vector<CNode*>& CNode::getChildren()'| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'CTree CTree::operator-(const string&)':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|94|error: 'std::__cxx11::string CNode::value' is private within this context| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CNode.h|24|note: declared private here| ||=== Build finished: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
- Assistant
- User
||=== Build: Release in lab5 (compiler: GNU GCC Compiler) ===| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'CNode* CTree::deepCopy(const CNode*) const':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|81|error: passing 'const CNode' as 'this' argument discards qualifiers [-fpermissive]| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CNode.h|15|note: in call to 'std::vector<CNode*>& CNode::getChildren()'| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'CTree CTree::operator-(const string&)':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|95|error: 'std::__cxx11::string CNode::value' is private within this context| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CNode.h|24|note: declared private here| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'void CTree::comp(const std::vector&)':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|219|error: expected primary-expression at end of input| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|219|error: expected ')' at end of input| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|219|error: expected statement at end of input| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|219|warning: this 'for' clause does not guard... [-Wmisleading-indentation]| ||note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|219|error: expected '}' at end of input| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|203|note: to match this '{'| ||=== Build finished: 6 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
- Assistant
- User
||=== Build: Release in lab5 (compiler: GNU GCC Compiler) ===| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'CNode* CTree::deepCopy(const CNode*) const':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|81|error: passing 'const CNode' as 'this' argument discards qualifiers [-fpermissive]| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CNode.h|15|note: in call to 'std::vector<CNode*>& CNode::getChildren()'| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp||In member function 'CTree CTree::operator-(const string&)':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|95|error: 'std::__cxx11::string CNode::value' is private within this context| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CNode.h|24|note: declared private here| ||=== Build finished: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| you can do deep copy useing enter(other.print) or something like this
- Assistant
- User
||=== Build: Debug in lab5 (compiler: GNU GCC Compiler) ===| obj\Debug\CTree.o||In function
CTree::parseFormula(std::queue<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::deque<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)':| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|131|undefined reference toCTree::isOperator(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)'| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|143|undefined reference toCTree::isNumber(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'| C:\Users\USER\OneDrive\Desktop\c++\uni\lab4\CTree.cpp|143|undefined reference toCTree::isVariable(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)'| ||error: ld returned 1 exit status| ||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| - Assistant