• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

부모클래스의 생성자 initializer_list 질문

21.12.08 23:07 작성 조회수 125

0

#include <iostream>
#include <cmath>
#include <limits>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdint>
#include <random>
#include <cstddef>
#include <array>
#include <tuple>
#include <cassert>
//#include <assert.h>
#include <algorithm>
#include <vector>
#include <chrono>
#include <initializer_list>

using namespace std;

template <typename T, int size>
class TemplateArrBase
{
protected:
	int m_length;
	T* m_arr;
public:
	TemplateArrBase(const int& value = 0) :
		m_length(value)
	{
		m_arr = new T[value];
	}

	TemplateArrBase(const initializer_list<T>& list)
	{
		resetArr(list.size());
		int count = 0;
		for (const auto& element : list)
		{
			m_arr[count] = element;
			count++;
		}
	}

	T& operator [](const int& index) const
	{

		assert(index < m_length);
		assert(index >= 0);
		
		return m_arr[index];
	}

	template<typename T>
	T sum() const
	{
		T sum = 0;
		for (int i = 0; i < m_length; i++)
		{
			sum += m_arr[i];
		}
		return sum;
	}

	//1.
	const int operator +(const TemplateArrBase& ref) const
	{
		return this->m_length + ref.m_length;
	}

	//3.
	const int operator +(const int &num) const
	{
		return m_length + num;
	}

	//2.
	friend const int operator+(int num, const TemplateArrBase& ref)
	{
		return num + ref.m_length;
	}

	friend ostream& operator <<(ostream& out, const TemplateArrBase& over)
	{
		for (int i = 0; i < over.m_length; i++)
		{
			out.operator<<(over.m_arr[i]) << " ";
		}
		cout << endl;
		return out;
	}

	//template <typename T>
	void setArr(const initializer_list<T>& arr)
	{
		resetArr(arr.size());
		int count = 0;
		for (const auto& element : arr)
		{
			m_arr[count] = element;
			count++;
		}
	}

	void operator = (const initializer_list<T>& list)
	{
		resetArr(list.size());
		int count = 0;
		for (const auto& element : list)
		{
			m_arr[count] = element;
			count++;
		}
	}

	void resetArr(const int& size)
	{
		delete[] m_arr;
		m_arr = new T[size];
		if (m_arr == nullptr)
		{
			assert(m_arr != nullptr);
		}
		m_length = size;
	}

	void print()
	{
		for (int i = 0; i < m_length; i++)
		{
			cout << m_arr[i] << " ";
		}
		cout << endl;
	}

	

	//friend const int operator+(const over& ref, const over& ref2)
	//{
	//	return ref.value + ref2.value;
	//}
};


template<typename T, int size>
class TemplateArr : public TemplateArrBase<T, size>
{

};

template<int size>
class TemplateArr<bool, size> : public TemplateArrBase<bool, size>
{
public:
	void losingTicket()
	{

	}
};

template<int size>
class TemplateArr<char, size> : public TemplateArrBase<char, size>
{
public:


	void print()
	{// protected 로 선언했는데 그냥 m_length 라고 쓰면 에러뜨는 이유가 템플릿이라서 그런가요?
		for (int i = 0; i < TemplateArrBase<char,size>::m_length; i++) 
		{
			cout << TemplateArrBase<char, size>::m_arr[i];
		}
		cout << endl;
	}
};

class A
{
public:
	A()
	{
		cout << "A" << endl;
	}
};

class B : public A
{
public:
	B()
	{
		cout << "B" << endl;
	}

	void test()
	{}
};

int main()
{
	TemplateArrBase<int,0> over = {1,2,3,4,5};
	over = { 1,2,3 };
	over[1] = 32;

	cout << over << endl;

	TemplateArr<bool,10> over2;
	over2.losingTicket();

	cout << over.sum<int>() << endl;
	over.print();

	TemplateArrBase<char,0> over3 = { 'e','r' };
	cout << over3 << endl;

	over3.print();

	TemplateArr<char, 0> over4 = { 'a','b'}; // 에러뜨는 이유를 모르겠습니다
	over4.print();


}

 

 

안녕하세요 위의 코드에 주석친것처럼 질문이 2가지가 있는데요

첫째가 템플릿이라서 상속받은 클래스들이 부모클래스의 멤버변수를 못찾는것인지에 대한 거랑

두번째로 아래 오류코드처럼 모클래스로는 initializer list 가 정상적으로 작동되는데 자식클래스로 초기화했을때는 왜 저런 에러가 뜨는지 모르겠습니다.

그래서 배열을 한개만 넣어서 초기화하면 정상작동은 하는데 문제가 문자들이 garbage 값들이 나옵니다.

모클래스 생성자도 상속받은건데 머가 문제인지 모르겠네요 ㅠㅠ

답변 1

답변을 작성해보세요.

0

안소님의 프로필

안소

2021.12.15

답변이 늦어 정말 죄송합니다 ㅠㅠ

저도 그 이유에 대해 잘 모르겠어서 틈틈히 구글링해보고 찾아보았는데 잘 모르겠네요.. 어렵네요 ㅠㅠ

두번째  이유도 첫번째 이유와 같은 이유로 에러가 나는 것일 것 같네요