静态数据成员
This commit is contained in:
parent
2f5e9cb82e
commit
4ad3a2dc7d
39
base/new/stringbad.cpp
Normal file
39
base/new/stringbad.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <cstring>
|
||||
#include "StringBad.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
int StringBad::num_strings = 0;
|
||||
|
||||
StringBad::StringBad(const char *s) {
|
||||
len = std::strlen(s);
|
||||
str = new char[len + 1];
|
||||
std::strcpy(str, s);
|
||||
num_strings++;
|
||||
cout << num_strings << " : \"" << str << "\" object created" << endl;
|
||||
}
|
||||
|
||||
StringBad::StringBad() {
|
||||
len = 4;
|
||||
str = new char[4];
|
||||
std::strcpy(str, "C++");
|
||||
num_strings++;
|
||||
cout << num_strings << " : \"" << str << "\" object created" << endl;
|
||||
}
|
||||
|
||||
StringBad::~StringBad() {
|
||||
cout << "\"" << str << "\" object deleted" << endl;
|
||||
--num_strings;
|
||||
cout << num_strings << " left" << endl;
|
||||
delete[] str;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const StringBad &st) {
|
||||
os << st.str;
|
||||
return os;
|
||||
}
|
23
base/new/stringbad.h
Normal file
23
base/new/stringbad.h
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/8/18.
|
||||
//
|
||||
|
||||
#ifndef BASE_STRINGBAD_H
|
||||
#define BASE_STRINGBAD_H
|
||||
#include <iostream>
|
||||
|
||||
class StringBad {
|
||||
|
||||
private:
|
||||
char * str;
|
||||
int len;
|
||||
static int num_strings;
|
||||
public:
|
||||
StringBad(const char * s);
|
||||
StringBad();
|
||||
~StringBad();
|
||||
friend std::ostream & operator<<(std::ostream & os,const StringBad & st);
|
||||
};
|
||||
|
||||
|
||||
#endif //BASE_STRINGBAD_H
|
49
base/new/vegnews.cpp
Normal file
49
base/new/vegnews.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "stringbad.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
void callme1(StringBad &);
|
||||
|
||||
void callme2(StringBad);
|
||||
|
||||
int main() {
|
||||
{
|
||||
cout << "Starting an inner block." << endl;
|
||||
StringBad headline1("Celery Stalk at Midnght");
|
||||
StringBad headline2("Lettuce Prey");
|
||||
StringBad sports("Spinach Leaves Bowl for Dollars");
|
||||
cout << "headline1: " << headline1 << endl;
|
||||
cout << "headline2: " << headline2 << endl;
|
||||
cout << "sports: " << sports << endl;
|
||||
callme1(headline1);
|
||||
cout << "headline1: " << headline1 << endl;
|
||||
callme2(headline2);
|
||||
cout << "headline2: " << headline2 << endl;
|
||||
cout << "Initialize one object to another:" << endl;
|
||||
StringBad sailor = sports;
|
||||
cout << "sailor: " << sailor << endl;
|
||||
cout << "Assign one object to another:\n";
|
||||
StringBad knot;
|
||||
knot = headline1;
|
||||
cout << "knot: " << knot << endl;
|
||||
cout << "Exiting the block." << endl;
|
||||
}
|
||||
cout << "End of main()" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void callme1(StringBad &rsb) {
|
||||
cout << "String passed by reference:" << endl;
|
||||
cout << " \"" << rsb << " \"" << endl;
|
||||
}
|
||||
|
||||
void callme2(StringBad sb) {
|
||||
cout << "String passed by value:" << endl;
|
||||
cout << " \"" << sb << " \"" << endl;
|
||||
}
|
Loading…
Reference in New Issue
Block a user