Coder Social home page Coder Social logo

nesingleinstance's Introduction

NeSingleInstance 单例和操作符重载

一、单例

Student.h

#ifndef Student_h
#define Student_h
class Student {
private:
    static Student* instance;
    Student();
public:
    static Student* getInstance();
    void sayHello();
};

#endif /* Student_h */

Student.cpp

#include <stdio.h>
#include <iostream>
#include "Student.h"
using namespace std;

Student* Student::instance = 0;
Student::Student() {
    
}

Student* Student::getInstance(){
    if(!instance) {
        instance = new Student();
    }
    return instance;
}

void Student::sayHello(){
    cout << "Hello 哈哈哈" << endl;
}

main.cpp

#include <iostream>
#include "Student.h"
using namespace std;

int main(int argc, const char * argv[]) {
    Student* student = Student::getInstance();
    student->sayHello();

    return 0;
}

二、函数重载

//函数重载
void func(int a) {
    
}
void func(float a) {
    
}

三、操作符重载

OperatorHeavyLoad.hpp

#ifndef OperatorHeavyLoad_hpp
#define OperatorHeavyLoad_hpp

#include <stdio.h>
class OperatorHeavyLoad {
public:
    int i;
    OperatorHeavyLoad operator+(const OperatorHeavyLoad& t) {
        OperatorHeavyLoad temp;
        temp.i = this->i + t.i;
        return temp;
    }
};
#endif /* OperatorHeavyLoad_hpp */

main.cpp

#include <iostream>
#include "OperatorHeavyLoad.hpp"
using namespace std;

int main(int argc, const char * argv[]) {
    OperatorHeavyLoad test1;
    test1.i = 100;
    OperatorHeavyLoad test2;
    test2.i = 200;
    //操作符重载
    OperatorHeavyLoad test3 = test1+test2;
    cout << "test3.i: " << test3.i << endl;
    return 0;
}

nesingleinstance's People

Contributors

tianyalu avatar

Watchers

 avatar  avatar

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.