tags:

views:

311

answers:

2

Hi, in code like this:

#pragma once
#include "stdafx.h"
#include "Token.h"

//I would like this enum to be inside class Number
enum Number_enm {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};

class Number : public Token<Number_enm>//and this template parameter to be Number::Number_enm
{
private:

public:
    Number(const Number_enm& num)
    try:
    Token(num)
    { }
    catch(...)
    {
     cerr << "Error in Number::Number(const Number_enm&).";
     return;
    }

    Number(const char num)
    try:
    Token(static_cast<Number_enm>(num & 0xf)) //get number value from char
    {
#ifdef DEBUG_
    cout << "Converting ctor, from char to Token.\n";
#endif
    }
    catch(...)
    {
     cerr << "Error in Number::Number(const char num).";
     return;
    }

};

#pragma once

/*Abstract class*/
template<class T>
class Token
{
    typedef T value_type;
private:
    value_type my_data_;
protected:
    /*Token()
    try: my_data_()
    { }
    catch(...)
    {
     cerr << "Error in Token<T>::Token().";
     return;
    }*/
    Token(const value_type value)
     try:
    my_data_(value)
    { }
    catch(...)
    {
     cerr << "Error in Token<T>::Token(const value_type&).";
     return;
    }
    /*Token(const Token& value): my_data(value)
    { }*/
    Token& operator=(const Token& right)
    {
#ifdef DEBUG_
     cout << "Token& operator=(const Token& right).\n";
#endif
     my_data = right;
     return my_data;
    }
public:
    T get() const
    {
     return my_data_;
    }
    T set(const T& new_value)
    {
     T old = my_data_;
     my_data_ = new_value;
     return old;
    }
};

I wonder if it is possible to do this kind of construction? Thank you for any help.

+1  A: 

No, you cannpt, because you can't forward declare an enum. Also, but not related, those contructor try blocks are a bad idea.

See also article by Herb Sutter at http://www.ddj.com/cpp/184401297 - he is slightly les condemnatory than me. Anyway, as I said, the try blocks have nothing to do with your question.

anon
Ok, thanks for your help. I read about the try catch block in ctor, in two books and one article (don't remember which one but it was article written by Scott Meyers), one book by Bjarne second by Meyers. There are adamant that this is a good idea, why are you saying that this is bad idea?
There is nothing we can do
anon
I'll take on board what you've said. Don't really have time, to go through this books to send you pages no's in which they explain that construction.
There is nothing we can do
A: 

It appears to me that you can, if you fix the errors.

Edit: Missed the comment, saying that you want the enum to be inside the class: you can't do that.

1) If you want to call the constructor of the base class then that is a template:

Number(const Number_enm& num)
    try:
    Token<Number_enm>(num)

2) You can't return from a constructor-level try block. You can only exit by (re)throwing - I assume not doing anything defaults to rethrowing.

Anyway, what kind of exceptions are you expecting from such simple types?

3) In one place you are referring to my_data instead of my_data_. But then, why do you implement the copy constructor and assignment operator if they have the default behavior? (The latter should return *this.)

UncleBens
Just like I've said in my previous comment to Neil, I've read about this construction and as I'm beginner I'm trying to use this construction to memorize it and make myself familiar with it.(Of course in this simple example I don't expect any exceptions to be thrown)
There is nothing we can do
Anyway, thank you for all your help
There is nothing we can do