将派生类保存在基类为静态类型的变量中

时间:2016-09-13 21:47:41

标签: c++ inheritance

我一直在互联网上到处寻找,并没有找到任何体面的答案来解决问题。 代码:

    <?php
if(ISSET($_POST['submit']){

        $username =$_POST['uname'];
        $password =$_POST['lpass'];

        $mdpass = md5($password);
        $result = mysql_query("select * from member where RegNo ='$username' and Password ='$mdpass'")or die(mysql_error());

        $count =mysql_num_rows($result);
        $row = mysql_fetch_array($result)

        if($count>0){

            session_start();  //start a session
            $_session['id']=$row['id']; //create a session variable
            header('location:home.php');  //redirect to home.php


        }
        else{
            header('location:index.php'); //redirect to index.php

        }

    }
?>

最后一行给出以下错误:无法分配抽象类型'Song'的对象

虽然,A级覆盖了play() - 方法,而且我正在分配A类的对象而不是P,我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

问题在于您试图通过将对象复制到P myVarA P P *myPtrVar = new A(); ... // Use myPtrVar here delete myPtrVar; std::unique_ptr<P> mySmartPtrVar(new A()); 中的其他实现。{/ p>

你不能用“价值”对象做到这一点;你需要一个指针或参考。基于内置C ++指针的解决方案如下所示:

delete

基于智能指针的解决方案如下所示:

mySmartPtrVar

使用slices off的好处是您无需调用using namespace std; template <typename T, int N> class BoundedBuffer { private: std::array<T, N> buffer; int read_pos; int write_pos; std::mutex reader_mutex; //mutex for between readers std::mutex writer_mutex; //mutex for between writers std::mutex shared_mutex; std::condition_variable reader_queue; std::condition_variable writer_queue; int timeout; //timeout in millisecond public: BoundedBuffer(const BoundedBuffer&) = delete; BoundedBuffer& operator=(const BoundedBuffer&) = delete; BoundedBuffer(int t) : read_pos(0), write_pos(0), timeout(t) { } inline bool empty() { return read_pos == write_pos; } inline bool full() { return write_pos >= read_pos + N; } bool put(const T& data) { unique_lock<mutex> writer_lock(writer_mutex); { unique_lock<mutex> shared_lock(shared_mutex); if (full()) { //buffer full if (writer_queue.wait_for(shared_lock, std::chrono::milliseconds(timeout)) == std::cv_status::timeout) return false; } } buffer[write_pos%N] = data; write_pos++; reader_queue.notify_one(); return true; } pair<T, bool> get() { unique_lock<mutex> reader_lock(reader_mutex); { unique_lock<mutex> shared_lock(shared_mutex); if (empty()) { //buffer empty if (reader_queue.wait_for(shared_lock, std::chrono::milliseconds(timeout)) == std::cv_status::timeout) { T t; return make_pair(t, false); } } } pair<T, bool> result = make_pair(buffer[read_pos%N], true); read_pos++; writer_queue.notify_one(); return result; } }; :只要TextViews超出范围,对象就会被释放。