Skip to content

luismiguelcasadodiaz/CPP_01

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPP_01

Automatic Destructor call

This code generates an automatic call to Weapon constructor

Weapon club = Weapon("crude spiked club");

//constructor call
HumanA bob("Bob", club);

//Constructor definition
HumanA(std::string onename, Weapon theweapon);

Otherway, this constructor definition does not call the Weapon constructor

HumanA::HumanA(std::string onename, Weapon& theweapon);

ex04 Replace

Attention here to validate access permission. You need to be sure that file has read permision. You need to be sure the folder where is located the readfila has write permision. You need to be sure that the write file you are going to create does not exist.

Additionaly, verify that original text to replace is not empty. Consider escape secuences in boht strings.

ex05 Pointers to class methods

I solved it thanks to this video

In function of the level, a differnt private function will be called.

		void _debug( void ) const;
		void _info( void ) const;
		void _warning( void ) const;
		void _error( void ) const;

At this level of course we can not use maps.

I use two arrays instead.

One, holds the capitalized level names "DEBUG", "INFO", "WARNING", "ERROR".It is an array of N strings.

other, holds pointers to methods using this syntax &Harl::method name. It is an array of pointers to methods.

IMHO this is the key for this project

 void (Harl::*_generic_f[N])(void) const;
 --v--                      ------v-----    
   |                              |
    \   Like methods signature    /

 void (Harl::*_generic_f[N])(void) const;
      ---v----                          
        |                             
         \   Pointer to something inside the class

 void (Harl::*_generic_f[N])(void) const;
                        -v-                         
                         |                             
                         \   Array definition wiht N elements

 void (Harl::*_generic_f[N])(void) const;
              ----v-----                          
                  |                             
                  \   attribute name

The class constructor carefully initilizes both arrays to correctly link level string with function pointers.

Harl::Harl(void)
{
   std::cout << "Harl Constructor called" << std::endl;
   this->_levels[0] = "DEBUG";
   this->_levels[1] = "INFO";
   this->_levels[2] = "WARNING";
   this->_levels[3] = "ERROR";		
   this->_generic_f[0] = &Harl::_debug;
   this->_generic_f[1] = &Harl::_info;
   this->_generic_f[2] = &Harl::_warning;
   this->_generic_f[3] = &Harl::_error;

}

generic function call

i loop _levels to find the right function_idx.

 		(this->*_generic_f[function_idx])();

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published