Hallo ihr Lieben,

Nach längerer Pause will ich mich wieder etwas mit C++ beschäftigen und ich habe kurz vor einigen Änderungen aufgehört, sprich alles ab C++11 sind für mich Neuerungen.

Ich schreibe grade an code für ein Programm und stehe vor mindestens einem Problem.
Das Programm soll eine Klasse bieten, die im Prinzip ein std::vector von Daten-Paaren darstellt. Diese Klasse soll allerdings nur Zeiger oder Referenzen enthalten, die auf existierende Objekte zeigen, denn ich will Konstruktoraufrufe und Neubildungen vermeiden.

Hier ist mal mein sicher nicht besonders hübscher Code:
Code:
#include <vector>
#include <string>
#include <iostream>
#include <memory> // unique_ptr


template <typename T,typename Arg>
T create(Arg&& a){
  return T(std::forward<Arg>(a));
}

// This class is a vector of data pairs.
// The vector's elements are pointers to already existing objects.
// Most important: the avoidance of new operator and memory allocation by accidential copying or creating temporary objects.
template <class T, class T2>
class cVecPair
{
	private:
		int lastAdded=-1; //< equals basically .size()-1
		// vectors
		std::vector<T* > one; 
		std::vector<T2* > two;
	public:
		enum pair{first, second}; // pair type differentiation, rather eye candy/syntax sugar
		// if size already estematable reserve it.
		cVecPair(int size=1) {
			one.reserve(size);
			two.reserve(size);
		}
		cVecPair(T& a, T2& b) {
			one.reserve(1);
			two.reserve(1);
			addPair(a,b);
		}
		
		// returns the index of the last added object
		int addPair(T& f, T2& s) {
			one.emplace_back(f);
			two.emplace_back(s);
			++lastAdded;
			return lastAdded;
		}
		
		// getter for elements in each pair
		auto getFirst(int index) const {
			return &one[index];
		}
		auto getSecond(int index) const {
			return &two[index];
		}
		
		auto getLastAdded(pair p) const {
			if(p==pair::first)
				return &getFirst(lastAdded);
			return &getSecond(lastAdded);
		}
};

// example class which is intended to be stored in another class which gets stored in cVecPair
class cSubClass {
	private:
		std::string name;
		const int ID;
	public:
		// normal c'tor
		cSubClass(auto x, auto y): name(x), ID(y) {};
		
		// getters
		std::string& getName() {
			return name;
		}
		
		auto getID() {
			return ID;
		}
};

// a complex class to be stored in cVecPair. No copies please.
class cPointInTime {
	private:
		static int id;
		const signed int ID;
		std::unique_ptr<cSubClass> sc;
	public:
		cPointInTime(const cPointInTime&) = delete;
		
		cPointInTime&& forward(typename std::remove_reference<cPointInTime>::type& a) noexcept
		{
			return static_cast<cPointInTime&&>(a);
		}
		
		cPointInTime(const std::string a) : ID(std::move(++id)) {
			sc={std::make_unique<cSubClass>(a,1)};
			}
		~cPointInTime() {
			std::cout << "Point in Time #" << ID << " gets destroyed." << std::endl;
		}
		
		auto get() {
			return sc.get();
		}
};
signed cPointInTime::id={0};

// Handles a cVecPair member.
class cManager {
	private:
		signed int adLst=0; // added last, at last, atlas
		cVecPair <std::string, cPointInTime > vecAr;
	public:
		cManager() {}
		void addVecPair(std::string&& a, cPointInTime& b) {
			adLst=vecAr.addPair(a, b);
		};
};

// Creates only one object of type cPointInTime, called Sub.
// A cManager object will put a reference or pointer  to Sub in its cVecPair object.
int main() {
	cPointInTime Sub("No copy please");
	cManager Man; //
	Man.addVecPair(Sub.get()->getName(), Sub);
};
Wie ihr seht habe ich hier viel rumprobiert, u.A. auch mit perfect forwarding und am Ende bin ich nur noch verwirrt.
Es wäre super für häufige Fälle von Zugriffen ein paar begründete Empfehlungen zu bekommen.

PS: Es gab im Gothic-Editing-Forum mal ein tool, welches daedalus code für das Forum eingefärbt hat, den hatte ich früher für Fragen rund um Code immer gerne genutzt. Hat noch jemand den Link dazu oder etwas, das auf C++ ausgelegt ist?


Grüße
Oparilames