Code Monkey home page Code Monkey logo

cpp's People

Contributors

ccwanggl avatar mlbo avatar shiqiyu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpp's Issues

Forget to explicitly define a virtual destructor in polymorphic base classes.

class Person
{
public:
string name;
Person(string n): name(n){}
virtual void print()
{
cout << "Name: " << name << endl;
}
};

The class Person doesn't have a user-written virtual destructor, so its destructor defaults to public non-virtual.

Person * p = new Student("xue", "2020");
p->print(); //if print() is not a virtual function, different output
delete p; //if its destructor is not virtual

The result of delete p; is undefined behavior, so it might call Person::~Person only and leak the string id .

In general, we should explicitly define a virtual destructor in polymorphic base classes. See C.127: A class with a virtual function should have a virtual or protected destructor .

派生类的拷贝构造函数的规范实现应该是调用基类的拷贝构造函数来进行初始化派生类对象的基类部分

Issue1

class MyMap: pubic MyString

派生访问说明符拼写错了,应该是 public 而不是 pubic

Issue2

MyMap(const MyMap & mm): MyString(mm.buf_len, mm.characters)
{
//allocate memory for keyname
//and hard copy from mm to *this
}

  1. 派生类的构造函数、拷贝构造函数和拷贝赋值运算符通常应该是 public 而不需要是 private

  2. 派生类的拷贝构造函数的规范实现应该是调用基类的拷贝构造函数来进行初始化派生类对象的基类部分,课件这里,调用基类的普通构造函数是不合适的,并且程序也是无法通过编译的,因为基类MyString的数据成员是 private 的,所以派生类MyMap是无法直接访问基类的 buf_lencharacters 数据成员的。

派生类MyMap的拷贝构造函数的规范实现代码如下:

public:
	MyMap(const MyMap& mm) : MyString(mm), keyname( /* initialize derived class members */)
	{
		// Do the rest of the copy constructor here...
	}

派生类的拷贝控制成员的规范实现示例代码如下:

#include <iostream>
#include <string>
#include <utility>

class Base
{
	// ...
public:
	Base() = default;
	Base(const Base&) = default;
	Base(Base&&) = default;
	Base& operator=(const Base&) = default;
	Base& operator=(Base&&) = default;
	virtual ~Base() = default;
	// ...
};

class Derived : public Base
{
	// ...
public:
	Derived() = default;

	Derived(const Derived& rhs) : Base{ rhs }, derived_{ rhs.derived_ }
	{
		// Do the rest of the copy constructor here...
	}

	// If the move constructor doesn't throw an exception, it should be marked noexcept.
	Derived(Derived&& rhs) noexcept : Base{ std::move(rhs) }, derived_{ std::move(rhs.derived_) }
	{
		// Do the rest of the move constructor here...
	}

	Derived& operator=(const Derived& rhs)
	{
		// The copy assignment operator can gracefully handle self-assignment.
		if (this != &rhs)
		{
			Base::operator=(rhs);
			derived_ = rhs.derived_;
			// Do the rest of the copy assignment operator here...
		}
		return *this;
	}

	// If the move assignment operator doesn't throw an exception, it should be marked noexcept.
	Derived& operator=(Derived&& rhs) noexcept
	{
		// The move assignment operator can gracefully handle self-assignment.
		if (this != &rhs)
		{
			Base::operator=(std::move(rhs));
			derived_ = std::move(rhs.derived_);
			// Do the rest of the move assignment operator here...
		}
		return *this;
	}

	~Derived() override = default;
private:
	std::string derived_;
	// ...
};

int main()
{
	Derived d0, d1, d2;
	Derived d3{ d0 };
	Derived d4{ std::move(d0) };
	d1 = d2;
	d1 = std::move(d2);
	return 0;
}

MyString's copy assignment operator can't handle self-assignment correctly and not exception safe

MyString & operator=(const MyString &ms)
{
create(ms.buf_len, ms.characters);
return *this;
}
bool create(int buf_len, const char * data)
{
release();
this->buf_len = buf_len;
if( this->buf_len != 0)
{
this->characters = new char[this->buf_len]{};
}
if(data)
strncpy(this->characters, data, this->buf_len);
return true;
}

Issue1: can't handle self-assignment

See: Assignment Operators, C++ FAQ (isocpp.org)
If x = x , bad errors will occur.
We can handle self-assignment by explicitly testing for self-assignment:

MyString& operator=(const MyString& ms)
{
    if (this != &ms)
    {
        create(ms.buf_len, ms.characters);
    }
    return *this;
}

Issue2: not exception safe

In create function, if new char[this->buf_len]{}; throws an exception, *this won't keep a valid state. So, copy assignment operator is not exception safe.
The solution is copy the underlying data firstly, then delete *this's old resource:

bool create(int buf_len, const char* data)
{
    char* new_characters = NULL;
    if (buf_len != 0)
    {
        new_characters = new char[buf_len] {};
    }
    if ((new_characters != NULL) && (data != NULL))
        strncpy(new_characters, data, buf_len);

    release();

    this->buf_len = buf_len;
    this->characters = new_characters;

    return true;
}

See also

[Bug]: Little bug in week08

At week08/examples/matoperation.cpp

float sum[8]={0};

this may lead to discontinues memory, use

float *sum = static_cast<float*>(aligned_alloc(256, 8*sizeof(float)));

instead may be better?

可以借助解引用操作变通地对 std::unique_ptr 进行深拷贝

// std::unique_ptr<MyTime> mt3 = mt1; // error

课件这里,其实可以这样进行拷贝: std::unique_ptr<MyTime> mt3 = std::make_unique<MyTime>(*mt1);

虽然 std::unique_ptr 删除了 copy constructor 和 copy assignment operator ,但其实我们可以借助解引用操作变通地对 std::unique_ptr 进行拷贝。

deep copy 示例如下:

std::unique_ptr<std::string> up1(std::make_unique<std::string>("Good morning"));

// copy construct!
std::unique_ptr<std::string> up2(std::make_unique<std::string>(*up1));
// safe copy construct!
std::unique_ptr<std::string> up3(up1 ? std::make_unique<std::string>(*up1) : nullptr);
// copy assignment!
up2 = std::make_unique<std::string>(*up1);
// safe copy assignment!
up3 = up1 ? std::make_unique<std::string>(*up1) : nullptr;

其它的例证:

Initialization on x86_64 platform

In my machine

$ uname -a 
Linux DELL-Inspiron7580 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

init.cpp output is

num1 = 0
num2 = 0

Lab内容与课程不对应?

感谢于老师带来精彩的课程。
一个小问题,为什么 lab 的内容与 lecture 内容不是对应的呢?比如说,week05 lecture 的内容是指针,但是 lab 却是 cmake

感谢于老师

感谢于老师无私的提供了如此精致细致的c++课程。(应该要有不少老教授们copy and finetune了)
在如今大学课堂上往往学不到东西,大学教授往往糊弄了事的现状里,于老师真是一股清流,谢谢您。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.