static_cast usage

Usage of static_cast
Syntax:
static_cast<type-name>(expression) The
conversion is legal only if type-name can be implicitly converted to the type that expression belongs to, or expression can be implicitly converted to the type that type-name belongs to. Otherwise, the compiler will report an error.
You can assign the address of an inherited derived class object to the base class pointer. Even if there is no virtual function in the base class, static_cast can be used for conversion.
The address of the inherited base class object can be assigned to the derived class pointer. Because derived class pointers can be implicitly converted to base class pointers without explicit type conversion, static_cast can be used to convert in the other direction, that is, to convert base class pointers to derived class pointers. But what is the point of doing this?
In the same way, because enumeration values ​​can be implicitly converted to integral types without explicit type conversion, static_cast can be used to convert integral types to enumeration types.
If the address of an object with no inheritance relationship is assigned to a pointer of another class, the compiler will report an error.
Please see code one:

#include<cstdio>
class Base{
  int dat;
public:
  explicit Base(int val) : dat(val){
    printf("%s, this=%p\n", __func__, this);
  }
#if 0
  virtual void act(){
#else
  void act(){
#endif
    printf("%s, this=%p\n", __func__, this);
  }
};
class Sub : public Base{
public:
  explicit Sub(int val) : Base(val){
    printf("%s, this=%p\n", __func__, this);
  }
};
class Other{
  int dat;
public:
  explicit Other(int val) : dat(val){
    printf("%s, this=%p\n", __func__, this);
  }
};
int main(){
  Base obase(1);
  Sub osub(2);
  Base *pbase = NULL;
  if(pbase = static_cast<Base *>(&osub) ){
    pbase->act();
  }
  
 printf("---------------\n");
  Base *ptr = &osub;
  ptr->act();
 printf("---------------\n");
#if 1
  if(Sub *psub = static_cast<Sub *>(&obase) ){
    psub->act();
  }
#endif
#if 0
  Other oother(3);
  //error: invalid static_cast from type ‘Other*’ to type ‘Base*’
  if(pbase = static_cast<Base *>(&oother) ){
    pbase->act();
  }
#endif 
}

Test Results:

frank @ userver: ~ / project / test / cpp / rtti $ ./a.out          

Base, this=0x7fff6d478100

Base, this=0x7fff6d478110

Sub, this=0x7fff6d478110

act, this=0x7fff6d478110

---------------

act, this=0x7fff6d478110

---------------

act, this=0x7fff6d478100

Code two:

#include<cstdio>
int main(){
  enum eSource{
    eAuxSource = 0,
    eOpticalSource,
    eBtSource,
    eFcSource,
    eSpotifySource,
    eGcSource
  };
#if 0
  eSource src = eBtSource;
#else
  //eSource src = 3; //error: invalid conversion from ‘int’ to ‘main()::eSource’ [-fpermissive]
  eSource src = static_cast<eSource>(3.1);
#endif
  printf("src: %p, %lu, %d\n", &src, sizeof(src), src);
}

 

Undertake programming in Matlab, Python and C++, machine learning, computer vision theory implementation and guidance, both undergraduate and master's degrees, salted fish trading, professional answers please go to know, please contact QQ number 757160542 for details, if you are the one.

 

 

Guess you like

Origin blog.csdn.net/weixin_36670529/article/details/115053523