浅学C++(1)

头文件

#include < iostream > 将文件复制黏贴到当前命令所在行
尖括号用于编译器的包含路径,”“双引号可以做一切

#pragma once
XXX

阻止单个头文件多次包含,并转换为当个翻译单元

#ifndef _LOG_H
#define _LOG_H
XXX
#endif

int main()

函数的入口,默认返回0

过程

cpp -> 翻译单元 -> obj(二进制) -> link -> exe

static

static int Multiply(int a,int b)
{
	return a * b;
}

使Multiply函数只被声明在当前翻译单元。
函数分为声明和定义。

引用与指针

int a = 1;
int& ref = a;
int* ptr = &a;

ref为引用,不是变量。
ptr为指针,它也是变量。

struct和class

默认访问类型区别
class默认访问类型private
struct默认访问类型public

单例模式

#include<iostream>


class Singleton
{
private:
	static Singleton* singleton ;

public:
	static Singleton& Get()
	{
		return *singleton;
	}
	void Hello()
	{
	}

};

Singleton* Singleton::singleton = nullptr;

int main()
{
	Singleton::Get().Hello();

	std::cin.get();
}

构造函数、析构函数


class Cat
{
private:
	float weight, height;

public:
	Cat(float w, float h)
	{
		weight = w;
		height = h;
	}
	~Cat()
	{
		std::cout << "destory" << std::endl;
	}
	void print()
	{
	std::cout << weight << "," << height << std::endl;
	}
};

void Function() {
	Cat cat(10.5, 20.3);
	cat.print();
}

int main()
{
	Function();
	std::cin.get();
}

继承

class Entity
{
public:
	float X, Y;
	void Move(float x, float y)
	{
		X += x;
		Y += y;
	}
};

class Player : public Entity
{
public:
	const char* name;
	void eat()
	{
		std::cout << "eating" << std::endl;
	}

};

虚函数

class Entity
{
public:
	virtual std::string GetName()
	{
		return "Entity";
	}
};

class Player : public Entity
{
private:
	std::string m_Name;
public:
	Player(const std::string& name)
		: m_Name(name){}
	std::string GetName() override
	{
		return m_Name;
	}
};

void PrintName(Entity* entity) {
	std::cout << entity->GetName() << std::endl;
}

int main()
{
	Entity* e = new Entity;
	PrintName(e);

	Player* p = new Player("Ch");
	PrintName(p);


	std::cin.get();
}

纯虚函数

class PrintTable
{
public:
	virtual std::string GetClassName() = 0;
};

字符串

	const char* name1 = "ZhangSan";  /*一个字节*/
	const wchar_t* name2 = L"ZhangSan";  /*windows两个 linux和mac都是四个*/
	const char16_t* name3 = u"ZhangSan";  /*两个字节*/
	const char32_t* name4 = U"ZhangSan";  /*三个字节*/

const int*、int const*、int* const

	const int* a = new int;/*不能修改值,能修改址*/
	int const* a = new int;/*不能修改值,能修改址*/
	int* const a = new int;/*能修改值,不能修改址*/
	const int* const a = new int;/*不能修改值,不能修改址*/

mutable

可修改

class Entity
{
private:
	std::string m_name;
	mutable int m_count = 0;
public:
	const std::string& GetName() const
	{
		m_count++;
		return m_name;
	}
};
int main()
{
	int x = 8;
	auto f = [=]() mutable
		{
			x++;/*复制值8*/
			std::cout << x << std::endl;/*输出9*/
		};
	f();
	std::cout << x << std::endl;/*输出8*/
	std::cin.get();
}

成员初始化列表

class Entity
{
private:
	std::string m_name;
	Example m_example;
public:
	Entity()
		: m_example(Example())
	{
		m_name = std::string("ZhangSan");
	}
};

成员变量初始化两次

class Entity
{
private:
	std::string m_name;
	Example m_example;
public:
	Entity()
	{
		m_name = std::string("ZhangSan");
		m_example = Example();
	}
};

成员变量初始化一次

成员变量不代表不会初始化

隐式转换

class Entity
{
private:
	std::string m_name;
	int m_age;
public:
	Entity(const std::string& name)
		: m_name(name),m_age(-1){}
	Entity(int age)
		: m_name("ZhangSan"), m_age(age) {}
};

void PrintEntity(const Entity& entity)
{

}

int main()
{
	PrintEntity(22);
	PrintEntity(std::string("ZhangSan"));
	Entity a = std::string("ZhangSan");
	Entity b = 20;
	std::cin.get();
}

explicit

class Entity
{
private:
	std::string m_name;
	int m_age;
public:
    Entity(const std::string& name)
		: m_name(name),m_age(-1){}
	explicit Entity(int age)
		: m_name("ZhangSan"), m_age(age) {}
};

不进行隐式转换

重载运算符

struct Vector2
{
	float x, y;
	Vector2(float x,float y)
		: x(x),y(y){}

	Vector2 Add(const Vector2& other) const
	{
		return Vector2(x + other.x, y + other.y);
	}
	Vector2 operator+(const Vector2& other) const
	{
		return Add(other);
	}

	Vector2 Multiple(const Vector2& other) const
	{
		return Vector2(x * other.x, y * other.y);
	}
	Vector2 operator*(const Vector2& other) const
	{
		return Multiple(other);
	}
};

std::ostream& operator<<(std::ostream& stream, const Vector2& other) {
	return stream << other.x << "," << other.y ;
	
}

int main()
{
	Vector2 position(4.0f, 4.0f);
	Vector2 speed(0.5f, 1.5f);
	Vector2 powerup(1.1f, 1.1f);

	Vector2 result = position.Add(speed.Multiple(powerup));

	Vector2 result2 = position + speed*powerup;
	std::cout << result2 << std::endl;
	std::cin.get();
}

栈的引用

class Entity
{
public:
	Entity()
	{
		std::cout << "create" << std::endl;
	};
	~Entity()
	{
		std::cout << "destory" << std::endl;
	};

};

class ScopedPtr
{
public:
	ScopedPtr(Entity* ptr)
	{
		this->ptr = ptr;
	};
	~ScopedPtr()
	{
		delete ptr;
	};

private:
	Entity* ptr;
};


int main()
{
	{
		ScopedPtr a = new Entity;
	}
	std::cin.get();
}

智能指针

#include<iostream>
#include<memory>
class Entity
{
public:
	Entity()
	{
		std::cout << "creating" << std::endl;
	};
	~Entity()
	{
		std::cout << "destorying" << std::endl;
	};

};

int main()
{
	{
		std::unique_ptr<Entity> entity = std::make_unique<Entity>();
	}
	std::cin.get();
}

共享指针

int main()
{
	{
		std::shared_ptr<Entity> entity1;
		{
			std::shared_ptr<Entity> entity = std::make_shared<Entity>();
			entity1 = entity;
		}
	}

	std::cin.get();
}

弱指针

int main()
{
	{
		std::weak_ptr<Entity> entity1;
		{
			std::shared_ptr<Entity> entity = std::make_shared<Entity>();
			entity1 = entity;
		}
	}

	std::cin.get();
}

拷贝构造函数

#include<iostream>
#include<string>
class String
{
private:
	char* m_buffer;
	unsigned int m_size;
public:
	String(const char* string)
	{
		m_size = strlen(string);
		m_buffer = new char[m_size+1];
		memcpy(m_buffer, string, m_size + 1 );
	};
	String(const String& other)
	{
		m_size = other.m_size;
		m_buffer = new char[m_size + 1];
		memcpy(m_buffer, other.m_buffer, m_size + 1);
	};
	~String()
	{
		delete[] m_buffer;
		std::cout << "destorying" << std::endl;
	};
	char& operator[](unsigned int index) 
	{
		return m_buffer[index];
	};
	friend std::ostream& operator<< (std::ostream& stream, const String& string);
};

std::ostream& operator<< (std::ostream& stream, const String& string)
{
	return stream << string.m_buffer ;
}

int main()
{
	String s = "temp";
	String t = s;
	t[2] = 'a';
	std::cout << s << std::endl;
	std::cout << t << std::endl;
	std::cin.get();
}

箭头获取偏移量

struct Vector3
{
	float x, y, z;
};

int main()
{
	int offer = (int)&((Vector3*)0)->y;
	std::cout << offer << std::endl;
	std::cin.get();
}
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在静态方法中使用JdbcTemplate需要注意以下几点: 1. 静态方法中无法直接使用Spring容器中的Bean,因为静态方法是类级别的,而Bean是实例级别的。因此需要手动获取JdbcTemplate实例,可以通过ApplicationContext获取JdbcTemplate实例,或者通过静态变量保存JdbcTemplate实例。 2. 在使用JdbcTemplate时,需要先创建一个JdbcTemplate实例,并设置数据源。数据源可以通过Spring容器注入,或者手动创建。在静态方法中,可以通过静态变量保存JdbcTemplate实例,避免重复创建。 3. 在使用JdbcTemplate操作数据库时,需要注意线程安全问题。JdbcTemplate是线程安全的,但是需要保证JdbcTemplate实例的线程安全,即在多线程环境中需要保证同一JdbcTemplate实例不会被并发访问。 下面是一个示例代码: ``` public class JdbcUtils { private static JdbcTemplate jdbcTemplate; public static void setDataSource(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); } public static void executeSql(String sql) { jdbcTemplate.execute(sql); } } ``` 在上面的代码中,我们通过静态变量保存了JdbcTemplate实例,并提供了一个静态方法setDataSource用于设置数据源。在使用JdbcTemplate时,我们可以直接调用静态方法executeSql执行SQL语句。需要注意的是,这里的executeSql方法是线程安全的,因为JdbcTemplate实例是共享的,并且JdbcTemplate本身是线程安全的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hi_heibao

谢谢您的鼓励与支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值