virtual destructor SYNTAX

This is a discussion on virtual destructor SYNTAX within the C++ Programming forums, part of the General Programming Boards category; Hi, i got a destructor for a class Base1. my syntax/format is wrong. can some-one correct it for me? Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    virtual destructor SYNTAX

    Hi, i got a destructor for a class Base1.
    my syntax/format is wrong. can some-one correct it for me?


    Code:
    Base1::virtual  ~Base1() { 
              cout << "Destroy Base" << endl;
    }
    Thanks,

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    All your qualifiers (eg: static, virtual, inline, etc) belong on the far left. Or, put another way, just as you declared it within the class with Base:: appended to the function name.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    i.e.
    Code:
    virtual Base1::~Base1()
    { 
              std::cout << "destroy all yer base" << std::endl;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    virtual outside class declaration

    hi, did this declaration for my virtual desctructor.it continues to give me this error:virtual outside class declaration using dev c++ compiler


    Code:
     #include <iostream>
     using namespace std;
     
    class Base1
    {
    private:
        int baseValue;
    public:
        Base1(int y):baseValue(y){} 
        virtual ~Base1();
    };
    
    virtual Base1::~Base1()
    { 
        std::cout << "destroy all yer base" << std::endl;
    }
    
    int main()
    {
    
    }

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CodeMonkey View Post
    i.e.
    Code:
    virtual Base1::~Base1()
    { 
              std::cout << "destroy all yer base" << std::endl;
    }
    Not quite. The virtual keyword can only be used in the class declaration, not in that type of definition. Compiling this on Comeau gives me:
    Code:
    "ComeauTest.c", line 8: error: invalid specifier outside a class declaration
      virtual A::~A()
      ^
    Code:
    class A
    {
      int m_A;
    public:
      virtual ~A();
    };
    
    virtual A::~A()  // Remove the virtual keyword here.
    {
      m_A = 5;
    }
    
    int main()
    {
      A a;
      return 0;
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Geez, what the hell was I thinking? I really need a vacation.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Whoops. I'll use Sebastiani's excuse.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 04:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 10:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21